Rainer Weikusat <[email protected]> writes:
> Ben Hutchings <[email protected]> writes:

[...]

>> unix: Fix potential double-unlock in unix_dgram_sendmsg()
>>
>> A datagram socket may be peered with itself, so that sk == other.  We
>> use unix_state_double_lock() to lock sk and other in the right order,
>> which also guards against this and only locks the socket once, but we
>> then end up trying to unlock it twice.  Add the check for sk != other.

[...]

> other was found via
>
>       if (!other) {
>               err = -ECONNRESET;
>               if (sunaddr == NULL)
>                       goto out_free;
>
>               other = unix_find_other(net, sunaddr, namelen, sk->sk_type,
>                                       hash, &err);
>               if (other == NULL)
>                       goto out_free;
>       }
>
> and the if-block leading to the double lock should never have been
> executed as it's supposed to deal with the case where sk is connect to
> other but other not to sk (eg, /dev/log).

Test program triggering a double unlock (w/o the MSG_DONTWAIT, it blocks
which it isn't supposed to do):

--------
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

#define SUN_NAME        "\0other-is-me"

int main(int argc, char **argv)
{
    struct sockaddr_un sun;
    unsigned max;
    int sk, rc;

    sk = socket(AF_UNIX, SOCK_DGRAM, 0);

    sun.sun_family = AF_UNIX;
    strncpy(sun.sun_path, SUN_NAME, sizeof(sun.sun_path));
    bind(sk, (struct sockaddr *)&sun, sizeof(sun));

    max = 12;
    do 
        sendto(sk, &sun, sizeof(sun), MSG_DONTWAIT,
               (struct sockaddr *)&sun, sizeof(sun));
    while (--max);

    return 0;
}

Reply via email to