You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Graham Dumpleton <gr...@apache.org> on 2014/05/31 05:54:15 UTC

Issue with connect() call made in mod_proxy_fdpass?

In mod_proxy_fdpass there is a function socket_connect_un():


https://svn.apache.org/repos/asf/httpd/httpd/trunk/modules/proxy/mod_proxy_fdpass.c

which contains the code:

        rv = connect(rawsock, (struct sockaddr*)sa,
                               sizeof(*sa) + strlen(sa->sun_path));

Can some explain to me why it is using:

sizeof(*sa) + strlen(sa->sun_path)

rather than just:

sizeof(*sa)

It just doesn't seem right.

One does find on the Internet examples which use:

#define SERV_PATH "./serv.path" struct sockaddr_un serv_addr; int servlen;
bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sun_family =
AF_UNIX; strcpy(serv_addr.sun_path, SERV_PATH); servlen =
strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family); connect(sockfd,
(struct sockaddr *) &serv_addr, servlen);

That is, the sockaddr_un structure length is calculated as:

servlen = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family)

It almost looked like someone started with something similar, but rather
than replace the whole thing with:

sizeof(*sa)

replaced just the part:

sizeof(serv_addr.sun_family)

and then wrongly still added the length of the sun_path member of the
struct to that.

Any comments? Is there something else funny going on with mod_proxy_fdpass
that requires it be done this way?

Graham

Re: Issue with connect() call made in mod_proxy_fdpass?

Posted by Graham Dumpleton <gr...@apache.org>.
Ahh, I am partly being a goose. I kept reading that strlen() as sizeof()
when reading the manual page. :-(

Graham


On 1 June 2014 21:44, Jeff Trawick <tr...@gmail.com> wrote:

> On Sun, Jun 1, 2014 at 3:10 AM, Graham Dumpleton <gr...@apache.org>
> wrote:
>
>> What I don't quite understand is why the Linux manual pages:
>>
>>   http://man7.org/linux/man-pages/man7/unix.7.html
>>
>> are even promoting the style:
>>
>>   offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1
>>
>> That would produce a length with is technically 1 greater than what the
>> size of sockaddr_un can be.
>>
>
> If sun_path has non-\0 in every position, it can be even larger depending
> on where the first \0 is ;)  But see below.
>
>
>>  I can only imagine that within connect() it must be deducting 1 for any
>> possible null byte and so allowing the last byte of sun_path to be a non
>> null byte and never actually checking the last byte as specified by the
>> length, which would be beyond the valid length of the structure.
>>
>
> It is possible (or potentially required) to manage the sockaddr_un storage
> allocation yourself and support longer filesystem paths than will fit in
> the preallocated sun_path field.  Exact limits vary by platform, but
> sun_path is generally declared smaller than the max supported path length.
>
> (See http://bugs.python.org/issue8882 for just one example discussion.)
>
> Thus the calculation of the sockaddr length based on actual data instead
> of the size of the structure, which may have to be mapped over a larger
> buffer in order to accommodate a more realistic path limit...
>
> (I think I alluded to this on the APR list.  APR really isn't helping
> appropriately if it doesn't handle this ugly issue.)
>
>
>> In the Apache code for mod_proxy_fdpass at least it doesn't really
>> matter, as when copying into sun_path it ensures that the last byte that
>> could ever be written is the last one possible in sun_path and that will
>> have a null byte. So there is no risk the Apache code could overrun
>> sun_path.
>>
>> I expect therefore that the Apache code could have simply used:
>>
>>   sizeof(struct sockaddr_un)
>>
>> As for the original issue with mod_proxy_fdpass, that was plain wrong and
>> on some Linux systems could result in connect() failing with error:
>>
>>   (22)Invalid argument
>>
>> as it would regard the supplied length as being over some internal limit
>> it checks. Doesn't happen on all Linux versions though based on what I have
>> had reported for some code of my own where I happened to replicate what
>> mod_proxy_fdpass was doing in that function.
>>
>> Graham
>>
>>
>>
>> On 1 June 2014 16:56, Christophe JAILLET <ch...@wanadoo.fr>
>> wrote:
>>
>>> Fix in r1598946.
>>>
>>> CJ
>>>
>>
>>
>
>
> --
> Born in Roswell... married an alien...
> http://emptyhammock.com/
> http://edjective.org/
>
>

Re: Issue with connect() call made in mod_proxy_fdpass?

Posted by Jeff Trawick <tr...@gmail.com>.
On Sun, Jun 1, 2014 at 7:44 AM, Jeff Trawick <tr...@gmail.com> wrote:

> On Sun, Jun 1, 2014 at 3:10 AM, Graham Dumpleton <gr...@apache.org>
> wrote:
>
>> What I don't quite understand is why the Linux manual pages:
>>
>>   http://man7.org/linux/man-pages/man7/unix.7.html
>>
>> are even promoting the style:
>>
>>   offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1
>>
>> That would produce a length with is technically 1 greater than what the
>> size of sockaddr_un can be.
>>
>
> If sun_path has non-\0 in every position, it can be even larger depending
> on where the first \0 is ;)  But see below.
>
>
>>  I can only imagine that within connect() it must be deducting 1 for any
>> possible null byte and so allowing the last byte of sun_path to be a non
>> null byte and never actually checking the last byte as specified by the
>> length, which would be beyond the valid length of the structure.
>>
>
> It is possible (or potentially required) to manage the sockaddr_un storage
> allocation yourself and support longer filesystem paths than will fit in
> the preallocated sun_path field.  Exact limits vary by platform, but
> sun_path is generally declared smaller than the max supported path length.
>
> (See http://bugs.python.org/issue8882 for just one example discussion.)
>
> Thus the calculation of the sockaddr length based on actual data instead
> of the size of the structure, which may have to be mapped over a larger
> buffer in order to accommodate a more realistic path limit...
>

Oh, and there is the SUN_LEN() macro which uses the strlen() flavor of
calculation...


> (I think I alluded to this on the APR list.  APR really isn't helping
> appropriately if it doesn't handle this ugly issue.)
>
>
>> In the Apache code for mod_proxy_fdpass at least it doesn't really
>> matter, as when copying into sun_path it ensures that the last byte that
>> could ever be written is the last one possible in sun_path and that will
>> have a null byte. So there is no risk the Apache code could overrun
>> sun_path.
>>
>> I expect therefore that the Apache code could have simply used:
>>
>>   sizeof(struct sockaddr_un)
>>
>> As for the original issue with mod_proxy_fdpass, that was plain wrong and
>> on some Linux systems could result in connect() failing with error:
>>
>>   (22)Invalid argument
>>
>> as it would regard the supplied length as being over some internal limit
>> it checks. Doesn't happen on all Linux versions though based on what I have
>> had reported for some code of my own where I happened to replicate what
>> mod_proxy_fdpass was doing in that function.
>>
>> Graham
>>
>>
>>
>> On 1 June 2014 16:56, Christophe JAILLET <ch...@wanadoo.fr>
>> wrote:
>>
>>> Fix in r1598946.
>>>
>>> CJ
>>>
>>
>>
>
>
> --
> Born in Roswell... married an alien...
> http://emptyhammock.com/
> http://edjective.org/
>
>


-- 
Born in Roswell... married an alien...
http://emptyhammock.com/
http://edjective.org/

Re: Issue with connect() call made in mod_proxy_fdpass?

Posted by Jeff Trawick <tr...@gmail.com>.
On Sun, Jun 1, 2014 at 3:10 AM, Graham Dumpleton <gr...@apache.org> wrote:

> What I don't quite understand is why the Linux manual pages:
>
>   http://man7.org/linux/man-pages/man7/unix.7.html
>
> are even promoting the style:
>
>   offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1
>
> That would produce a length with is technically 1 greater than what the
> size of sockaddr_un can be.
>

If sun_path has non-\0 in every position, it can be even larger depending
on where the first \0 is ;)  But see below.


> I can only imagine that within connect() it must be deducting 1 for any
> possible null byte and so allowing the last byte of sun_path to be a non
> null byte and never actually checking the last byte as specified by the
> length, which would be beyond the valid length of the structure.
>

It is possible (or potentially required) to manage the sockaddr_un storage
allocation yourself and support longer filesystem paths than will fit in
the preallocated sun_path field.  Exact limits vary by platform, but
sun_path is generally declared smaller than the max supported path length.

(See http://bugs.python.org/issue8882 for just one example discussion.)

Thus the calculation of the sockaddr length based on actual data instead of
the size of the structure, which may have to be mapped over a larger buffer
in order to accommodate a more realistic path limit...

(I think I alluded to this on the APR list.  APR really isn't helping
appropriately if it doesn't handle this ugly issue.)


> In the Apache code for mod_proxy_fdpass at least it doesn't really matter,
> as when copying into sun_path it ensures that the last byte that could ever
> be written is the last one possible in sun_path and that will have a null
> byte. So there is no risk the Apache code could overrun sun_path.
>
> I expect therefore that the Apache code could have simply used:
>
>   sizeof(struct sockaddr_un)
>
> As for the original issue with mod_proxy_fdpass, that was plain wrong and
> on some Linux systems could result in connect() failing with error:
>
>   (22)Invalid argument
>
> as it would regard the supplied length as being over some internal limit
> it checks. Doesn't happen on all Linux versions though based on what I have
> had reported for some code of my own where I happened to replicate what
> mod_proxy_fdpass was doing in that function.
>
> Graham
>
>
>
> On 1 June 2014 16:56, Christophe JAILLET <ch...@wanadoo.fr>
> wrote:
>
>> Fix in r1598946.
>>
>> CJ
>>
>
>


-- 
Born in Roswell... married an alien...
http://emptyhammock.com/
http://edjective.org/

Re: Issue with connect() call made in mod_proxy_fdpass?

Posted by Graham Dumpleton <gr...@apache.org>.
What I don't quite understand is why the Linux manual pages:

  http://man7.org/linux/man-pages/man7/unix.7.html

are even promoting the style:

  offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1

That would produce a length with is technically 1 greater than what the
size of sockaddr_un can be. I can only imagine that within connect() it
must be deducting 1 for any possible null byte and so allowing the last
byte of sun_path to be a non null byte and never actually checking the last
byte as specified by the length, which would be beyond the valid length of
the structure.

In the Apache code for mod_proxy_fdpass at least it doesn't really matter,
as when copying into sun_path it ensures that the last byte that could ever
be written is the last one possible in sun_path and that will have a null
byte. So there is no risk the Apache code could overrun sun_path.

I expect therefore that the Apache code could have simply used:

  sizeof(struct sockaddr_un)

As for the original issue with mod_proxy_fdpass, that was plain wrong and
on some Linux systems could result in connect() failing with error:

  (22)Invalid argument

as it would regard the supplied length as being over some internal limit it
checks. Doesn't happen on all Linux versions though based on what I have
had reported for some code of my own where I happened to replicate what
mod_proxy_fdpass was doing in that function.

Graham



On 1 June 2014 16:56, Christophe JAILLET <ch...@wanadoo.fr>
wrote:

> Fix in r1598946.
>
> CJ
>

Re: Issue with connect() call made in mod_proxy_fdpass?

Posted by Christophe JAILLET <ch...@wanadoo.fr>.
Fix in r1598946.

CJ

Re: Issue with connect() call made in mod_proxy_fdpass?

Posted by Christophe JAILLET <ch...@wanadoo.fr>.
There is also the same kind of code in apr/misc/unix/rand.c.
In this case, the +1 is missing.

I think that line 157 of 'rand.c' should be changed in:
         egd_path_len = strlen(*egdsockname) + 1;
in order to take into account the '\0' that is expedted at the end of 
sun_path.

CJ


Le 31/05/2014 08:34, Christophe JAILLET a écrit :
> Hi,
>
> I share your feeling.
>
> See line 2572 of proxy_util.c, where the length is computed as follow:
>
>         const socklen_t addrlen = APR_OFFSETOF(struct sockaddr_un, 
> sun_path)
>                                   + strlen(sa->sun_path) + 1;
>         rv = connect(rawsock, (struct sockaddr*)sa, addrlen);
>
>
> Apart that, the 2 'socket_connect_un' functions (in mod_proxy_fdpass.c 
> <https://svn.apache.org/repos/asf/httpd/httpd/trunk/modules/proxy/mod_proxy_fdpass.c> 
> and in proxy_util.c) are the same.
>
> CJ
>
>
>
> Le 31/05/2014 05:54, Graham Dumpleton a écrit :
>> In mod_proxy_fdpass there is a function socket_connect_un():
>>
>> https://svn.apache.org/repos/asf/httpd/httpd/trunk/modules/proxy/mod_proxy_fdpass.c 
>>
>>
>> which contains the code:
>>
>>         rv = connect(rawsock, (struct sockaddr*)sa,
>>      sizeof(*sa) + strlen(sa->sun_path));
>>
>> Can some explain to me why it is using:
>>
>> sizeof(*sa) + strlen(sa->sun_path)
>>
>> rather than just:
>>
>> sizeof(*sa)
>>
>> It just doesn't seem right.
>>
>> One does find on the Internet examples which use:
>>
>> #define SERV_PATH "./serv.path" struct sockaddr_un serv_addr; int 
>> servlen; bzero((char *) &serv_addr, sizeof(serv_addr)); 
>> serv_addr.sun_family = AF_UNIX; strcpy(serv_addr.sun_path, 
>> SERV_PATH); servlen = strlen(serv_addr.sun_path) + 
>> sizeof(serv_addr.sun_family); connect(sockfd, (struct sockaddr *) 
>> &serv_addr, servlen);
>>
>> That is, the sockaddr_un structure length is calculated as:
>>
>> servlen = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family)
>>
>> It almost looked like someone started with something similar, but 
>> rather than replace the whole thing with:
>>
>> sizeof(*sa)
>>
>> replaced just the part:
>>
>> sizeof(serv_addr.sun_family)
>>
>> and then wrongly still added the length of the sun_path member of the 
>> struct to that.
>>
>> Any comments? Is there something else funny going on with 
>> mod_proxy_fdpass that requires it be done this way?
>>
>> Graham
>>
>>
>>
>
>


Re: Issue with connect() call made in mod_proxy_fdpass?

Posted by Christophe JAILLET <ch...@wanadoo.fr>.
Hi,

I share your feeling.

See line 2572 of proxy_util.c, where the length is computed as follow:

         const socklen_t addrlen = APR_OFFSETOF(struct sockaddr_un, 
sun_path)
                                   + strlen(sa->sun_path) + 1;
         rv = connect(rawsock, (struct sockaddr*)sa, addrlen);


Apart that, the 2 'socket_connect_un' functions (in mod_proxy_fdpass.c 
<https://svn.apache.org/repos/asf/httpd/httpd/trunk/modules/proxy/mod_proxy_fdpass.c> 
and in proxy_util.c) are the same.

CJ



Le 31/05/2014 05:54, Graham Dumpleton a écrit :
> In mod_proxy_fdpass there is a function socket_connect_un():
>
> https://svn.apache.org/repos/asf/httpd/httpd/trunk/modules/proxy/mod_proxy_fdpass.c
>
> which contains the code:
>
>         rv = connect(rawsock, (struct sockaddr*)sa,
>      sizeof(*sa) + strlen(sa->sun_path));
>
> Can some explain to me why it is using:
>
> sizeof(*sa) + strlen(sa->sun_path)
>
> rather than just:
>
> sizeof(*sa)
>
> It just doesn't seem right.
>
> One does find on the Internet examples which use:
>
> #define SERV_PATH "./serv.path" struct sockaddr_un serv_addr; int 
> servlen; bzero((char *) &serv_addr, sizeof(serv_addr)); 
> serv_addr.sun_family = AF_UNIX; strcpy(serv_addr.sun_path, SERV_PATH); 
> servlen = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family); 
> connect(sockfd, (struct sockaddr *) &serv_addr, servlen);
>
> That is, the sockaddr_un structure length is calculated as:
>
> servlen = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family)
>
> It almost looked like someone started with something similar, but 
> rather than replace the whole thing with:
>
> sizeof(*sa)
>
> replaced just the part:
>
> sizeof(serv_addr.sun_family)
>
> and then wrongly still added the length of the sun_path member of the 
> struct to that.
>
> Any comments? Is there something else funny going on with 
> mod_proxy_fdpass that requires it be done this way?
>
> Graham
>
>
>



Re: Issue with connect() call made in mod_proxy_fdpass?

Posted by Christophe JAILLET <ch...@wanadoo.fr>.
Hi,

I share your feeling.

See line 2572 of proxy_util.c, where the length is computed as follow:

         const socklen_t addrlen = APR_OFFSETOF(struct sockaddr_un, 
sun_path)
                                   + strlen(sa->sun_path) + 1;
         rv = connect(rawsock, (struct sockaddr*)sa, addrlen);


Apart that, the 2 'socket_connect_un' functions (in mod_proxy_fdpass.c 
<https://svn.apache.org/repos/asf/httpd/httpd/trunk/modules/proxy/mod_proxy_fdpass.c> 
and in proxy_util.c) are the same.

CJ



Le 31/05/2014 05:54, Graham Dumpleton a écrit :
> In mod_proxy_fdpass there is a function socket_connect_un():
>
> https://svn.apache.org/repos/asf/httpd/httpd/trunk/modules/proxy/mod_proxy_fdpass.c
>
> which contains the code:
>
>         rv = connect(rawsock, (struct sockaddr*)sa,
>      sizeof(*sa) + strlen(sa->sun_path));
>
> Can some explain to me why it is using:
>
> sizeof(*sa) + strlen(sa->sun_path)
>
> rather than just:
>
> sizeof(*sa)
>
> It just doesn't seem right.
>
> One does find on the Internet examples which use:
>
> #define SERV_PATH "./serv.path" struct sockaddr_un serv_addr; int 
> servlen; bzero((char *) &serv_addr, sizeof(serv_addr)); 
> serv_addr.sun_family = AF_UNIX; strcpy(serv_addr.sun_path, SERV_PATH); 
> servlen = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family); 
> connect(sockfd, (struct sockaddr *) &serv_addr, servlen);
>
> That is, the sockaddr_un structure length is calculated as:
>
> servlen = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family)
>
> It almost looked like someone started with something similar, but 
> rather than replace the whole thing with:
>
> sizeof(*sa)
>
> replaced just the part:
>
> sizeof(serv_addr.sun_family)
>
> and then wrongly still added the length of the sun_path member of the 
> struct to that.
>
> Any comments? Is there something else funny going on with 
> mod_proxy_fdpass that requires it be done this way?
>
> Graham
>
>
>