<feed xmlns='http://www.w3.org/2005/Atom'>
<title>linux-toradex.git/net/netlink, branch v4.2.7</title>
<subtitle>Linux kernel for Apalis and Colibri modules</subtitle>
<link rel='alternate' type='text/html' href='https://git.toradex.cn/cgit/linux-toradex.git/'/>
<entry>
<title>netlink: fix locking around NETLINK_LIST_MEMBERSHIPS</title>
<updated>2015-12-09T19:31:03+00:00</updated>
<author>
<name>David Herrmann</name>
<email>dh.herrmann@gmail.com</email>
</author>
<published>2015-10-21T09:47:43+00:00</published>
<link rel='alternate' type='text/html' href='https://git.toradex.cn/cgit/linux-toradex.git/commit/?id=aa23ddc33c318841c3e856665e4c7fd0b432a1f5'/>
<id>aa23ddc33c318841c3e856665e4c7fd0b432a1f5</id>
<content type='text'>
[ Upstream commit 47191d65b647af5eb5c82ede70ed4c24b1e93ef4 ]

Currently, NETLINK_LIST_MEMBERSHIPS grabs the netlink table while copying
the membership state to user-space. However, grabing the netlink table is
effectively a write_lock_irq(), and as such we should not be triggering
page-faults in the critical section.

This can be easily reproduced by the following snippet:
    int s = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
    void *p = mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
    int r = getsockopt(s, 0x10e, 9, p, (void*)((char*)p + 4092));

This should work just fine, but currently triggers EFAULT and a possible
WARN_ON below handle_mm_fault().

Fix this by reducing locking of NETLINK_LIST_MEMBERSHIPS to a read-side
lock. The write-lock was overkill in the first place, and the read-lock
allows page-faults just fine.

Reported-by: Dmitry Vyukov &lt;dvyukov@google.com&gt;
Signed-off-by: David Herrmann &lt;dh.herrmann@gmail.com&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
[ Upstream commit 47191d65b647af5eb5c82ede70ed4c24b1e93ef4 ]

Currently, NETLINK_LIST_MEMBERSHIPS grabs the netlink table while copying
the membership state to user-space. However, grabing the netlink table is
effectively a write_lock_irq(), and as such we should not be triggering
page-faults in the critical section.

This can be easily reproduced by the following snippet:
    int s = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
    void *p = mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
    int r = getsockopt(s, 0x10e, 9, p, (void*)((char*)p + 4092));

This should work just fine, but currently triggers EFAULT and a possible
WARN_ON below handle_mm_fault().

Fix this by reducing locking of NETLINK_LIST_MEMBERSHIPS to a read-side
lock. The write-lock was overkill in the first place, and the read-lock
allows page-faults just fine.

Reported-by: Dmitry Vyukov &lt;dvyukov@google.com&gt;
Signed-off-by: David Herrmann &lt;dh.herrmann@gmail.com&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>netlink: Trim skb to alloc size to avoid MSG_TRUNC</title>
<updated>2015-10-27T00:53:37+00:00</updated>
<author>
<name>Arad, Ronen</name>
<email>ronen.arad@intel.com</email>
</author>
<published>2015-10-15T08:55:17+00:00</published>
<link rel='alternate' type='text/html' href='https://git.toradex.cn/cgit/linux-toradex.git/commit/?id=326ce2c1bc95805e724aced6a557e1adfce2b8f2'/>
<id>326ce2c1bc95805e724aced6a557e1adfce2b8f2</id>
<content type='text'>
[ Upstream commit db65a3aaf29ecce2e34271d52e8d2336b97bd9fe ]

netlink_dump() allocates skb based on the calculated min_dump_alloc or
a per socket max_recvmsg_len.
min_alloc_size is maximum space required for any single netdev
attributes as calculated by rtnl_calcit().
max_recvmsg_len tracks the user provided buffer to netlink_recvmsg.
It is capped at 16KiB.
The intention is to avoid small allocations and to minimize the number
of calls required to obtain dump information for all net devices.

netlink_dump packs as many small messages as could fit within an skb
that was sized for the largest single netdev information. The actual
space available within an skb is larger than what is requested. It could
be much larger and up to near 2x with align to next power of 2 approach.

Allowing netlink_dump to use all the space available within the
allocated skb increases the buffer size a user has to provide to avoid
truncaion (i.e. MSG_TRUNG flag set).

It was observed that with many VLANs configured on at least one netdev,
a larger buffer of near 64KiB was necessary to avoid "Message truncated"
error in "ip link" or "bridge [-c[ompressvlans]] vlan show" when
min_alloc_size was only little over 32KiB.

This patch trims skb to allocated size in order to allow the user to
avoid truncation with more reasonable buffer size.

Signed-off-by: Ronen Arad &lt;ronen.arad@intel.com&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
[ Upstream commit db65a3aaf29ecce2e34271d52e8d2336b97bd9fe ]

netlink_dump() allocates skb based on the calculated min_dump_alloc or
a per socket max_recvmsg_len.
min_alloc_size is maximum space required for any single netdev
attributes as calculated by rtnl_calcit().
max_recvmsg_len tracks the user provided buffer to netlink_recvmsg.
It is capped at 16KiB.
The intention is to avoid small allocations and to minimize the number
of calls required to obtain dump information for all net devices.

netlink_dump packs as many small messages as could fit within an skb
that was sized for the largest single netdev information. The actual
space available within an skb is larger than what is requested. It could
be much larger and up to near 2x with align to next power of 2 approach.

Allowing netlink_dump to use all the space available within the
allocated skb increases the buffer size a user has to provide to avoid
truncaion (i.e. MSG_TRUNG flag set).

It was observed that with many VLANs configured on at least one netdev,
a larger buffer of near 64KiB was necessary to avoid "Message truncated"
error in "ip link" or "bridge [-c[ompressvlans]] vlan show" when
min_alloc_size was only little over 32KiB.

This patch trims skb to allocated size in order to allow the user to
avoid truncation with more reasonable buffer size.

Signed-off-by: Ronen Arad &lt;ronen.arad@intel.com&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>netlink: Replace rhash_portid with bound</title>
<updated>2015-10-03T11:51:38+00:00</updated>
<author>
<name>Herbert Xu</name>
<email>herbert@gondor.apana.org.au</email>
</author>
<published>2015-09-22T03:38:56+00:00</published>
<link rel='alternate' type='text/html' href='https://git.toradex.cn/cgit/linux-toradex.git/commit/?id=e2a3131de43c6e8072ed618330c49f14d87dba6e'/>
<id>e2a3131de43c6e8072ed618330c49f14d87dba6e</id>
<content type='text'>
[ Upstream commit da314c9923fed553a007785a901fd395b7eb6c19 ]

On Mon, Sep 21, 2015 at 02:20:22PM -0400, Tejun Heo wrote:
&gt;
&gt; store_release and load_acquire are different from the usual memory
&gt; barriers and can't be paired this way.  You have to pair store_release
&gt; and load_acquire.  Besides, it isn't a particularly good idea to

OK I've decided to drop the acquire/release helpers as they don't
help us at all and simply pessimises the code by using full memory
barriers (on some architectures) where only a write or read barrier
is needed.

&gt; depend on memory barriers embedded in other data structures like the
&gt; above.  Here, especially, rhashtable_insert() would have write barrier
&gt; *before* the entry is hashed not necessarily *after*, which means that
&gt; in the above case, a socket which appears to have set bound to a
&gt; reader might not visible when the reader tries to look up the socket
&gt; on the hashtable.

But you are right we do need an explicit write barrier here to
ensure that the hashing is visible.

&gt; There's no reason to be overly smart here.  This isn't a crazy hot
&gt; path, write barriers tend to be very cheap, store_release more so.
&gt; Please just do smp_store_release() and note what it's paired with.

It's not about being overly smart.  It's about actually understanding
what's going on with the code.  I've seen too many instances of
people simply sprinkling synchronisation primitives around without
any knowledge of what is happening underneath, which is just a recipe
for creating hard-to-debug races.

&gt; &gt; @@ -1539,7 +1546,7 @@ static int netlink_bind(struct socket *sock, struct sockaddr *addr,
&gt; &gt;  		}
&gt; &gt;  	}
&gt; &gt;
&gt; &gt; -	if (!nlk-&gt;portid) {
&gt; &gt; +	if (!nlk-&gt;bound) {
&gt;
&gt; I don't think you can skip load_acquire here just because this is the
&gt; second deref of the variable.  That doesn't change anything.  Race
&gt; condition could still happen between the first and second tests and
&gt; skipping the second would lead to the same kind of bug.

The reason this one is OK is because we do not use nlk-&gt;portid or
try to get nlk from the hash table before we return to user-space.

However, there is a real bug here that none of these acquire/release
helpers discovered.  The two bound tests here used to be a single
one.  Now that they are separate it is entirely possible for another
thread to come in the middle and bind the socket.  So we need to
repeat the portid check in order to maintain consistency.

&gt; &gt; @@ -1587,7 +1594,7 @@ static int netlink_connect(struct socket *sock, struct sockaddr *addr,
&gt; &gt;  	    !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
&gt; &gt;  		return -EPERM;
&gt; &gt;
&gt; &gt; -	if (!nlk-&gt;portid)
&gt; &gt; +	if (!nlk-&gt;bound)
&gt;
&gt; Don't we need load_acquire here too?  Is this path holding a lock
&gt; which makes that unnecessary?

Ditto.

---8&lt;---
The commit 1f770c0a09da855a2b51af6d19de97fb955eca85 ("netlink:
Fix autobind race condition that leads to zero port ID") created
some new races that can occur due to inconcsistencies between the
two port IDs.

Tejun is right that a barrier is unavoidable.  Therefore I am
reverting to the original patch that used a boolean to indicate
that a user netlink socket has been bound.

Barriers have been added where necessary to ensure that a valid
portid and the hashed socket is visible.

I have also changed netlink_insert to only return EBUSY if the
socket is bound to a portid different to the requested one.  This
combined with only reading nlk-&gt;bound once in netlink_bind fixes
a race where two threads that bind the socket at the same time
with different port IDs may both succeed.

Fixes: 1f770c0a09da ("netlink: Fix autobind race condition that leads to zero port ID")
Reported-by: Tejun Heo &lt;tj@kernel.org&gt;
Reported-by: Linus Torvalds &lt;torvalds@linux-foundation.org&gt;
Signed-off-by: Herbert Xu &lt;herbert@gondor.apana.org.au&gt;
Nacked-by: Tejun Heo &lt;tj@kernel.org&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
[ Upstream commit da314c9923fed553a007785a901fd395b7eb6c19 ]

On Mon, Sep 21, 2015 at 02:20:22PM -0400, Tejun Heo wrote:
&gt;
&gt; store_release and load_acquire are different from the usual memory
&gt; barriers and can't be paired this way.  You have to pair store_release
&gt; and load_acquire.  Besides, it isn't a particularly good idea to

OK I've decided to drop the acquire/release helpers as they don't
help us at all and simply pessimises the code by using full memory
barriers (on some architectures) where only a write or read barrier
is needed.

&gt; depend on memory barriers embedded in other data structures like the
&gt; above.  Here, especially, rhashtable_insert() would have write barrier
&gt; *before* the entry is hashed not necessarily *after*, which means that
&gt; in the above case, a socket which appears to have set bound to a
&gt; reader might not visible when the reader tries to look up the socket
&gt; on the hashtable.

But you are right we do need an explicit write barrier here to
ensure that the hashing is visible.

&gt; There's no reason to be overly smart here.  This isn't a crazy hot
&gt; path, write barriers tend to be very cheap, store_release more so.
&gt; Please just do smp_store_release() and note what it's paired with.

It's not about being overly smart.  It's about actually understanding
what's going on with the code.  I've seen too many instances of
people simply sprinkling synchronisation primitives around without
any knowledge of what is happening underneath, which is just a recipe
for creating hard-to-debug races.

&gt; &gt; @@ -1539,7 +1546,7 @@ static int netlink_bind(struct socket *sock, struct sockaddr *addr,
&gt; &gt;  		}
&gt; &gt;  	}
&gt; &gt;
&gt; &gt; -	if (!nlk-&gt;portid) {
&gt; &gt; +	if (!nlk-&gt;bound) {
&gt;
&gt; I don't think you can skip load_acquire here just because this is the
&gt; second deref of the variable.  That doesn't change anything.  Race
&gt; condition could still happen between the first and second tests and
&gt; skipping the second would lead to the same kind of bug.

The reason this one is OK is because we do not use nlk-&gt;portid or
try to get nlk from the hash table before we return to user-space.

However, there is a real bug here that none of these acquire/release
helpers discovered.  The two bound tests here used to be a single
one.  Now that they are separate it is entirely possible for another
thread to come in the middle and bind the socket.  So we need to
repeat the portid check in order to maintain consistency.

&gt; &gt; @@ -1587,7 +1594,7 @@ static int netlink_connect(struct socket *sock, struct sockaddr *addr,
&gt; &gt;  	    !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
&gt; &gt;  		return -EPERM;
&gt; &gt;
&gt; &gt; -	if (!nlk-&gt;portid)
&gt; &gt; +	if (!nlk-&gt;bound)
&gt;
&gt; Don't we need load_acquire here too?  Is this path holding a lock
&gt; which makes that unnecessary?

Ditto.

---8&lt;---
The commit 1f770c0a09da855a2b51af6d19de97fb955eca85 ("netlink:
Fix autobind race condition that leads to zero port ID") created
some new races that can occur due to inconcsistencies between the
two port IDs.

Tejun is right that a barrier is unavoidable.  Therefore I am
reverting to the original patch that used a boolean to indicate
that a user netlink socket has been bound.

Barriers have been added where necessary to ensure that a valid
portid and the hashed socket is visible.

I have also changed netlink_insert to only return EBUSY if the
socket is bound to a portid different to the requested one.  This
combined with only reading nlk-&gt;bound once in netlink_bind fixes
a race where two threads that bind the socket at the same time
with different port IDs may both succeed.

Fixes: 1f770c0a09da ("netlink: Fix autobind race condition that leads to zero port ID")
Reported-by: Tejun Heo &lt;tj@kernel.org&gt;
Reported-by: Linus Torvalds &lt;torvalds@linux-foundation.org&gt;
Signed-off-by: Herbert Xu &lt;herbert@gondor.apana.org.au&gt;
Nacked-by: Tejun Heo &lt;tj@kernel.org&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>netlink: Fix autobind race condition that leads to zero port ID</title>
<updated>2015-10-03T11:51:37+00:00</updated>
<author>
<name>Herbert Xu</name>
<email>herbert@gondor.apana.org.au</email>
</author>
<published>2015-09-18T11:16:50+00:00</published>
<link rel='alternate' type='text/html' href='https://git.toradex.cn/cgit/linux-toradex.git/commit/?id=6e32e731184134db406c428f491a9811cf58252a'/>
<id>6e32e731184134db406c428f491a9811cf58252a</id>
<content type='text'>
[ Upstream commit 1f770c0a09da855a2b51af6d19de97fb955eca85 ]

The commit c0bb07df7d981e4091432754e30c9c720e2c0c78 ("netlink:
Reset portid after netlink_insert failure") introduced a race
condition where if two threads try to autobind the same socket
one of them may end up with a zero port ID.  This led to kernel
deadlocks that were observed by multiple people.

This patch reverts that commit and instead fixes it by introducing
a separte rhash_portid variable so that the real portid is only set
after the socket has been successfully hashed.

Fixes: c0bb07df7d98 ("netlink: Reset portid after netlink_insert failure")
Reported-by: Tejun Heo &lt;tj@kernel.org&gt;
Reported-by: Linus Torvalds &lt;torvalds@linux-foundation.org&gt;
Signed-off-by: Herbert Xu &lt;herbert@gondor.apana.org.au&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
[ Upstream commit 1f770c0a09da855a2b51af6d19de97fb955eca85 ]

The commit c0bb07df7d981e4091432754e30c9c720e2c0c78 ("netlink:
Reset portid after netlink_insert failure") introduced a race
condition where if two threads try to autobind the same socket
one of them may end up with a zero port ID.  This led to kernel
deadlocks that were observed by multiple people.

This patch reverts that commit and instead fixes it by introducing
a separte rhash_portid variable so that the real portid is only set
after the socket has been successfully hashed.

Fixes: c0bb07df7d98 ("netlink: Reset portid after netlink_insert failure")
Reported-by: Tejun Heo &lt;tj@kernel.org&gt;
Reported-by: Linus Torvalds &lt;torvalds@linux-foundation.org&gt;
Signed-off-by: Herbert Xu &lt;herbert@gondor.apana.org.au&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>netlink, mmap: transform mmap skb into full skb on taps</title>
<updated>2015-10-03T11:51:36+00:00</updated>
<author>
<name>Daniel Borkmann</name>
<email>daniel@iogearbox.net</email>
</author>
<published>2015-09-10T18:05:46+00:00</published>
<link rel='alternate' type='text/html' href='https://git.toradex.cn/cgit/linux-toradex.git/commit/?id=62f43b58d2b2c4f0200b9ca2b997f4c484f0272f'/>
<id>62f43b58d2b2c4f0200b9ca2b997f4c484f0272f</id>
<content type='text'>
[ Upstream commit 1853c949646005b5959c483becde86608f548f24 ]

Ken-ichirou reported that running netlink in mmap mode for receive in
combination with nlmon will throw a NULL pointer dereference in
__kfree_skb() on nlmon_xmit(), in my case I can also trigger an "unable
to handle kernel paging request". The problem is the skb_clone() in
__netlink_deliver_tap_skb() for skbs that are mmaped.

I.e. the cloned skb doesn't have a destructor, whereas the mmap netlink
skb has it pointed to netlink_skb_destructor(), set in the handler
netlink_ring_setup_skb(). There, skb-&gt;head is being set to NULL, so
that in such cases, __kfree_skb() doesn't perform a skb_release_data()
via skb_release_all(), where skb-&gt;head is possibly being freed through
kfree(head) into slab allocator, although netlink mmap skb-&gt;head points
to the mmap buffer. Similarly, the same has to be done also for large
netlink skbs where the data area is vmalloced. Therefore, as discussed,
make a copy for these rather rare cases for now. This fixes the issue
on my and Ken-ichirou's test-cases.

Reference: http://thread.gmane.org/gmane.linux.network/371129
Fixes: bcbde0d449ed ("net: netlink: virtual tap device management")
Reported-by: Ken-ichirou MATSUZAWA &lt;chamaken@gmail.com&gt;
Signed-off-by: Daniel Borkmann &lt;daniel@iogearbox.net&gt;
Tested-by: Ken-ichirou MATSUZAWA &lt;chamaken@gmail.com&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
[ Upstream commit 1853c949646005b5959c483becde86608f548f24 ]

Ken-ichirou reported that running netlink in mmap mode for receive in
combination with nlmon will throw a NULL pointer dereference in
__kfree_skb() on nlmon_xmit(), in my case I can also trigger an "unable
to handle kernel paging request". The problem is the skb_clone() in
__netlink_deliver_tap_skb() for skbs that are mmaped.

I.e. the cloned skb doesn't have a destructor, whereas the mmap netlink
skb has it pointed to netlink_skb_destructor(), set in the handler
netlink_ring_setup_skb(). There, skb-&gt;head is being set to NULL, so
that in such cases, __kfree_skb() doesn't perform a skb_release_data()
via skb_release_all(), where skb-&gt;head is possibly being freed through
kfree(head) into slab allocator, although netlink mmap skb-&gt;head points
to the mmap buffer. Similarly, the same has to be done also for large
netlink skbs where the data area is vmalloced. Therefore, as discussed,
make a copy for these rather rare cases for now. This fixes the issue
on my and Ken-ichirou's test-cases.

Reference: http://thread.gmane.org/gmane.linux.network/371129
Fixes: bcbde0d449ed ("net: netlink: virtual tap device management")
Reported-by: Ken-ichirou MATSUZAWA &lt;chamaken@gmail.com&gt;
Signed-off-by: Daniel Borkmann &lt;daniel@iogearbox.net&gt;
Tested-by: Ken-ichirou MATSUZAWA &lt;chamaken@gmail.com&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>netlink: mmap: fix tx type check</title>
<updated>2015-08-23T23:04:46+00:00</updated>
<author>
<name>Ken-ichirou MATSUZAWA</name>
<email>chamaken@gmail.com</email>
</author>
<published>2015-08-20T03:43:53+00:00</published>
<link rel='alternate' type='text/html' href='https://git.toradex.cn/cgit/linux-toradex.git/commit/?id=c953e23936f902c7719166327e3113639105c981'/>
<id>c953e23936f902c7719166327e3113639105c981</id>
<content type='text'>
I can't send netlink message via mmaped netlink socket since

    commit: a8866ff6a5bce7d0ec465a63bc482a85c09b0d39
    netlink: make the check for "send from tx_ring" deterministic

msg-&gt;msg_iter.type is set to WRITE (1) at

    SYSCALL_DEFINE6(sendto, ...
        import_single_range(WRITE, ...
            iov_iter_init(1, WRITE, ...

call path, so that we need to check the type by iter_is_iovec()
to accept the WRITE.

Signed-off-by: Ken-ichirou MATSUZAWA &lt;chamas@h4.dion.ne.jp&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
I can't send netlink message via mmaped netlink socket since

    commit: a8866ff6a5bce7d0ec465a63bc482a85c09b0d39
    netlink: make the check for "send from tx_ring" deterministic

msg-&gt;msg_iter.type is set to WRITE (1) at

    SYSCALL_DEFINE6(sendto, ...
        import_single_range(WRITE, ...
            iov_iter_init(1, WRITE, ...

call path, so that we need to check the type by iter_is_iovec()
to accept the WRITE.

Signed-off-by: Ken-ichirou MATSUZAWA &lt;chamas@h4.dion.ne.jp&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>netlink: make sure -EBUSY won't escape from netlink_insert</title>
<updated>2015-08-10T17:59:10+00:00</updated>
<author>
<name>Daniel Borkmann</name>
<email>daniel@iogearbox.net</email>
</author>
<published>2015-08-06T22:26:41+00:00</published>
<link rel='alternate' type='text/html' href='https://git.toradex.cn/cgit/linux-toradex.git/commit/?id=4e7c1330689e27556de407d3fdadc65ffff5eb12'/>
<id>4e7c1330689e27556de407d3fdadc65ffff5eb12</id>
<content type='text'>
Linus reports the following deadlock on rtnl_mutex; triggered only
once so far (extract):

[12236.694209] NetworkManager  D 0000000000013b80     0  1047      1 0x00000000
[12236.694218]  ffff88003f902640 0000000000000000 ffffffff815d15a9 0000000000000018
[12236.694224]  ffff880119538000 ffff88003f902640 ffffffff81a8ff84 00000000ffffffff
[12236.694230]  ffffffff81a8ff88 ffff880119c47f00 ffffffff815d133a ffffffff81a8ff80
[12236.694235] Call Trace:
[12236.694250]  [&lt;ffffffff815d15a9&gt;] ? schedule_preempt_disabled+0x9/0x10
[12236.694257]  [&lt;ffffffff815d133a&gt;] ? schedule+0x2a/0x70
[12236.694263]  [&lt;ffffffff815d15a9&gt;] ? schedule_preempt_disabled+0x9/0x10
[12236.694271]  [&lt;ffffffff815d2c3f&gt;] ? __mutex_lock_slowpath+0x7f/0xf0
[12236.694280]  [&lt;ffffffff815d2cc6&gt;] ? mutex_lock+0x16/0x30
[12236.694291]  [&lt;ffffffff814f1f90&gt;] ? rtnetlink_rcv+0x10/0x30
[12236.694299]  [&lt;ffffffff8150ce3b&gt;] ? netlink_unicast+0xfb/0x180
[12236.694309]  [&lt;ffffffff814f5ad3&gt;] ? rtnl_getlink+0x113/0x190
[12236.694319]  [&lt;ffffffff814f202a&gt;] ? rtnetlink_rcv_msg+0x7a/0x210
[12236.694331]  [&lt;ffffffff8124565c&gt;] ? sock_has_perm+0x5c/0x70
[12236.694339]  [&lt;ffffffff814f1fb0&gt;] ? rtnetlink_rcv+0x30/0x30
[12236.694346]  [&lt;ffffffff8150d62c&gt;] ? netlink_rcv_skb+0x9c/0xc0
[12236.694354]  [&lt;ffffffff814f1f9f&gt;] ? rtnetlink_rcv+0x1f/0x30
[12236.694360]  [&lt;ffffffff8150ce3b&gt;] ? netlink_unicast+0xfb/0x180
[12236.694367]  [&lt;ffffffff8150d344&gt;] ? netlink_sendmsg+0x484/0x5d0
[12236.694376]  [&lt;ffffffff810a236f&gt;] ? __wake_up+0x2f/0x50
[12236.694387]  [&lt;ffffffff814cad23&gt;] ? sock_sendmsg+0x33/0x40
[12236.694396]  [&lt;ffffffff814cb05e&gt;] ? ___sys_sendmsg+0x22e/0x240
[12236.694405]  [&lt;ffffffff814cab75&gt;] ? ___sys_recvmsg+0x135/0x1a0
[12236.694415]  [&lt;ffffffff811a9d12&gt;] ? eventfd_write+0x82/0x210
[12236.694423]  [&lt;ffffffff811a0f9e&gt;] ? fsnotify+0x32e/0x4c0
[12236.694429]  [&lt;ffffffff8108cb70&gt;] ? wake_up_q+0x60/0x60
[12236.694434]  [&lt;ffffffff814cba09&gt;] ? __sys_sendmsg+0x39/0x70
[12236.694440]  [&lt;ffffffff815d4797&gt;] ? entry_SYSCALL_64_fastpath+0x12/0x6a

It seems so far plausible that the recursive call into rtnetlink_rcv()
looks suspicious. One way, where this could trigger is that the senders
NETLINK_CB(skb).portid was wrongly 0 (which is rtnetlink socket), so
the rtnl_getlink() request's answer would be sent to the kernel instead
to the actual user process, thus grabbing rtnl_mutex() twice.

One theory would be that netlink_autobind() triggered via netlink_sendmsg()
internally overwrites the -EBUSY error to 0, but where it is wrongly
originating from __netlink_insert() instead. That would reset the
socket's portid to 0, which is then filled into NETLINK_CB(skb).portid
later on. As commit d470e3b483dc ("[NETLINK]: Fix two socket hashing bugs.")
also puts it, -EBUSY should not be propagated from netlink_insert().

It looks like it's very unlikely to reproduce. We need to trigger the
rhashtable_insert_rehash() handler under a situation where rehashing
currently occurs (one /rare/ way would be to hit ht-&gt;elasticity limits
while not filled enough to expand the hashtable, but that would rather
require a specifically crafted bind() sequence with knowledge about
destination slots, seems unlikely). It probably makes sense to guard
__netlink_insert() in any case and remap that error. It was suggested
that EOVERFLOW might be better than an already overloaded ENOMEM.

Reference: http://thread.gmane.org/gmane.linux.network/372676
Reported-by: Linus Torvalds &lt;torvalds@linux-foundation.org&gt;
Signed-off-by: Daniel Borkmann &lt;daniel@iogearbox.net&gt;
Acked-by: Herbert Xu &lt;herbert@gondor.apana.org.au&gt;
Acked-by: Thomas Graf &lt;tgraf@suug.ch&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Linus reports the following deadlock on rtnl_mutex; triggered only
once so far (extract):

[12236.694209] NetworkManager  D 0000000000013b80     0  1047      1 0x00000000
[12236.694218]  ffff88003f902640 0000000000000000 ffffffff815d15a9 0000000000000018
[12236.694224]  ffff880119538000 ffff88003f902640 ffffffff81a8ff84 00000000ffffffff
[12236.694230]  ffffffff81a8ff88 ffff880119c47f00 ffffffff815d133a ffffffff81a8ff80
[12236.694235] Call Trace:
[12236.694250]  [&lt;ffffffff815d15a9&gt;] ? schedule_preempt_disabled+0x9/0x10
[12236.694257]  [&lt;ffffffff815d133a&gt;] ? schedule+0x2a/0x70
[12236.694263]  [&lt;ffffffff815d15a9&gt;] ? schedule_preempt_disabled+0x9/0x10
[12236.694271]  [&lt;ffffffff815d2c3f&gt;] ? __mutex_lock_slowpath+0x7f/0xf0
[12236.694280]  [&lt;ffffffff815d2cc6&gt;] ? mutex_lock+0x16/0x30
[12236.694291]  [&lt;ffffffff814f1f90&gt;] ? rtnetlink_rcv+0x10/0x30
[12236.694299]  [&lt;ffffffff8150ce3b&gt;] ? netlink_unicast+0xfb/0x180
[12236.694309]  [&lt;ffffffff814f5ad3&gt;] ? rtnl_getlink+0x113/0x190
[12236.694319]  [&lt;ffffffff814f202a&gt;] ? rtnetlink_rcv_msg+0x7a/0x210
[12236.694331]  [&lt;ffffffff8124565c&gt;] ? sock_has_perm+0x5c/0x70
[12236.694339]  [&lt;ffffffff814f1fb0&gt;] ? rtnetlink_rcv+0x30/0x30
[12236.694346]  [&lt;ffffffff8150d62c&gt;] ? netlink_rcv_skb+0x9c/0xc0
[12236.694354]  [&lt;ffffffff814f1f9f&gt;] ? rtnetlink_rcv+0x1f/0x30
[12236.694360]  [&lt;ffffffff8150ce3b&gt;] ? netlink_unicast+0xfb/0x180
[12236.694367]  [&lt;ffffffff8150d344&gt;] ? netlink_sendmsg+0x484/0x5d0
[12236.694376]  [&lt;ffffffff810a236f&gt;] ? __wake_up+0x2f/0x50
[12236.694387]  [&lt;ffffffff814cad23&gt;] ? sock_sendmsg+0x33/0x40
[12236.694396]  [&lt;ffffffff814cb05e&gt;] ? ___sys_sendmsg+0x22e/0x240
[12236.694405]  [&lt;ffffffff814cab75&gt;] ? ___sys_recvmsg+0x135/0x1a0
[12236.694415]  [&lt;ffffffff811a9d12&gt;] ? eventfd_write+0x82/0x210
[12236.694423]  [&lt;ffffffff811a0f9e&gt;] ? fsnotify+0x32e/0x4c0
[12236.694429]  [&lt;ffffffff8108cb70&gt;] ? wake_up_q+0x60/0x60
[12236.694434]  [&lt;ffffffff814cba09&gt;] ? __sys_sendmsg+0x39/0x70
[12236.694440]  [&lt;ffffffff815d4797&gt;] ? entry_SYSCALL_64_fastpath+0x12/0x6a

It seems so far plausible that the recursive call into rtnetlink_rcv()
looks suspicious. One way, where this could trigger is that the senders
NETLINK_CB(skb).portid was wrongly 0 (which is rtnetlink socket), so
the rtnl_getlink() request's answer would be sent to the kernel instead
to the actual user process, thus grabbing rtnl_mutex() twice.

One theory would be that netlink_autobind() triggered via netlink_sendmsg()
internally overwrites the -EBUSY error to 0, but where it is wrongly
originating from __netlink_insert() instead. That would reset the
socket's portid to 0, which is then filled into NETLINK_CB(skb).portid
later on. As commit d470e3b483dc ("[NETLINK]: Fix two socket hashing bugs.")
also puts it, -EBUSY should not be propagated from netlink_insert().

It looks like it's very unlikely to reproduce. We need to trigger the
rhashtable_insert_rehash() handler under a situation where rehashing
currently occurs (one /rare/ way would be to hit ht-&gt;elasticity limits
while not filled enough to expand the hashtable, but that would rather
require a specifically crafted bind() sequence with knowledge about
destination slots, seems unlikely). It probably makes sense to guard
__netlink_insert() in any case and remap that error. It was suggested
that EOVERFLOW might be better than an already overloaded ENOMEM.

Reference: http://thread.gmane.org/gmane.linux.network/372676
Reported-by: Linus Torvalds &lt;torvalds@linux-foundation.org&gt;
Signed-off-by: Daniel Borkmann &lt;daniel@iogearbox.net&gt;
Acked-by: Herbert Xu &lt;herbert@gondor.apana.org.au&gt;
Acked-by: Thomas Graf &lt;tgraf@suug.ch&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>netlink: don't hold mutex in rcu callback when releasing mmapd ring</title>
<updated>2015-07-22T05:22:56+00:00</updated>
<author>
<name>Florian Westphal</name>
<email>fw@strlen.de</email>
</author>
<published>2015-07-21T14:33:50+00:00</published>
<link rel='alternate' type='text/html' href='https://git.toradex.cn/cgit/linux-toradex.git/commit/?id=0470eb99b4721586ccac954faac3fa4472da0845'/>
<id>0470eb99b4721586ccac954faac3fa4472da0845</id>
<content type='text'>
Kirill A. Shutemov says:

This simple test-case trigers few locking asserts in kernel:

int main(int argc, char **argv)
{
        unsigned int block_size = 16 * 4096;
        struct nl_mmap_req req = {
                .nm_block_size          = block_size,
                .nm_block_nr            = 64,
                .nm_frame_size          = 16384,
                .nm_frame_nr            = 64 * block_size / 16384,
        };
        unsigned int ring_size;
	int fd;

	fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
        if (setsockopt(fd, SOL_NETLINK, NETLINK_RX_RING, &amp;req, sizeof(req)) &lt; 0)
                exit(1);
        if (setsockopt(fd, SOL_NETLINK, NETLINK_TX_RING, &amp;req, sizeof(req)) &lt; 0)
                exit(1);

	ring_size = req.nm_block_nr * req.nm_block_size;
	mmap(NULL, 2 * ring_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
	return 0;
}

+++ exited with 0 +++
BUG: sleeping function called from invalid context at /home/kas/git/public/linux-mm/kernel/locking/mutex.c:616
in_atomic(): 1, irqs_disabled(): 0, pid: 1, name: init
3 locks held by init/1:
 #0:  (reboot_mutex){+.+...}, at: [&lt;ffffffff81080959&gt;] SyS_reboot+0xa9/0x220
 #1:  ((reboot_notifier_list).rwsem){.+.+..}, at: [&lt;ffffffff8107f379&gt;] __blocking_notifier_call_chain+0x39/0x70
 #2:  (rcu_callback){......}, at: [&lt;ffffffff810d32e0&gt;] rcu_do_batch.isra.49+0x160/0x10c0
Preemption disabled at:[&lt;ffffffff8145365f&gt;] __delay+0xf/0x20

CPU: 1 PID: 1 Comm: init Not tainted 4.1.0-00009-gbddf4c4818e0 #253
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS Debian-1.8.2-1 04/01/2014
 ffff88017b3d8000 ffff88027bc03c38 ffffffff81929ceb 0000000000000102
 0000000000000000 ffff88027bc03c68 ffffffff81085a9d 0000000000000002
 ffffffff81ca2a20 0000000000000268 0000000000000000 ffff88027bc03c98
Call Trace:
 &lt;IRQ&gt;  [&lt;ffffffff81929ceb&gt;] dump_stack+0x4f/0x7b
 [&lt;ffffffff81085a9d&gt;] ___might_sleep+0x16d/0x270
 [&lt;ffffffff81085bed&gt;] __might_sleep+0x4d/0x90
 [&lt;ffffffff8192e96f&gt;] mutex_lock_nested+0x2f/0x430
 [&lt;ffffffff81932fed&gt;] ? _raw_spin_unlock_irqrestore+0x5d/0x80
 [&lt;ffffffff81464143&gt;] ? __this_cpu_preempt_check+0x13/0x20
 [&lt;ffffffff8182fc3d&gt;] netlink_set_ring+0x1ed/0x350
 [&lt;ffffffff8182e000&gt;] ? netlink_undo_bind+0x70/0x70
 [&lt;ffffffff8182fe20&gt;] netlink_sock_destruct+0x80/0x150
 [&lt;ffffffff817e484d&gt;] __sk_free+0x1d/0x160
 [&lt;ffffffff817e49a9&gt;] sk_free+0x19/0x20
[..]

Cong Wang says:

We can't hold mutex lock in a rcu callback, [..]

Thomas Graf says:

The socket should be dead at this point. It might be simpler to
add a netlink_release_ring() function which doesn't require
locking at all.

Reported-by: "Kirill A. Shutemov" &lt;kirill@shutemov.name&gt;
Diagnosed-by: Cong Wang &lt;cwang@twopensource.com&gt;
Suggested-by: Thomas Graf &lt;tgraf@suug.ch&gt;
Signed-off-by: Florian Westphal &lt;fw@strlen.de&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Kirill A. Shutemov says:

This simple test-case trigers few locking asserts in kernel:

int main(int argc, char **argv)
{
        unsigned int block_size = 16 * 4096;
        struct nl_mmap_req req = {
                .nm_block_size          = block_size,
                .nm_block_nr            = 64,
                .nm_frame_size          = 16384,
                .nm_frame_nr            = 64 * block_size / 16384,
        };
        unsigned int ring_size;
	int fd;

	fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
        if (setsockopt(fd, SOL_NETLINK, NETLINK_RX_RING, &amp;req, sizeof(req)) &lt; 0)
                exit(1);
        if (setsockopt(fd, SOL_NETLINK, NETLINK_TX_RING, &amp;req, sizeof(req)) &lt; 0)
                exit(1);

	ring_size = req.nm_block_nr * req.nm_block_size;
	mmap(NULL, 2 * ring_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
	return 0;
}

+++ exited with 0 +++
BUG: sleeping function called from invalid context at /home/kas/git/public/linux-mm/kernel/locking/mutex.c:616
in_atomic(): 1, irqs_disabled(): 0, pid: 1, name: init
3 locks held by init/1:
 #0:  (reboot_mutex){+.+...}, at: [&lt;ffffffff81080959&gt;] SyS_reboot+0xa9/0x220
 #1:  ((reboot_notifier_list).rwsem){.+.+..}, at: [&lt;ffffffff8107f379&gt;] __blocking_notifier_call_chain+0x39/0x70
 #2:  (rcu_callback){......}, at: [&lt;ffffffff810d32e0&gt;] rcu_do_batch.isra.49+0x160/0x10c0
Preemption disabled at:[&lt;ffffffff8145365f&gt;] __delay+0xf/0x20

CPU: 1 PID: 1 Comm: init Not tainted 4.1.0-00009-gbddf4c4818e0 #253
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS Debian-1.8.2-1 04/01/2014
 ffff88017b3d8000 ffff88027bc03c38 ffffffff81929ceb 0000000000000102
 0000000000000000 ffff88027bc03c68 ffffffff81085a9d 0000000000000002
 ffffffff81ca2a20 0000000000000268 0000000000000000 ffff88027bc03c98
Call Trace:
 &lt;IRQ&gt;  [&lt;ffffffff81929ceb&gt;] dump_stack+0x4f/0x7b
 [&lt;ffffffff81085a9d&gt;] ___might_sleep+0x16d/0x270
 [&lt;ffffffff81085bed&gt;] __might_sleep+0x4d/0x90
 [&lt;ffffffff8192e96f&gt;] mutex_lock_nested+0x2f/0x430
 [&lt;ffffffff81932fed&gt;] ? _raw_spin_unlock_irqrestore+0x5d/0x80
 [&lt;ffffffff81464143&gt;] ? __this_cpu_preempt_check+0x13/0x20
 [&lt;ffffffff8182fc3d&gt;] netlink_set_ring+0x1ed/0x350
 [&lt;ffffffff8182e000&gt;] ? netlink_undo_bind+0x70/0x70
 [&lt;ffffffff8182fe20&gt;] netlink_sock_destruct+0x80/0x150
 [&lt;ffffffff817e484d&gt;] __sk_free+0x1d/0x160
 [&lt;ffffffff817e49a9&gt;] sk_free+0x19/0x20
[..]

Cong Wang says:

We can't hold mutex lock in a rcu callback, [..]

Thomas Graf says:

The socket should be dead at this point. It might be simpler to
add a netlink_release_ring() function which doesn't require
locking at all.

Reported-by: "Kirill A. Shutemov" &lt;kirill@shutemov.name&gt;
Diagnosed-by: Cong Wang &lt;cwang@twopensource.com&gt;
Suggested-by: Thomas Graf &lt;tgraf@suug.ch&gt;
Signed-off-by: Florian Westphal &lt;fw@strlen.de&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>netlink: Delete an unnecessary check before the function call "module_put"</title>
<updated>2015-07-03T16:27:43+00:00</updated>
<author>
<name>Markus Elfring</name>
<email>elfring@users.sourceforge.net</email>
</author>
<published>2015-07-02T16:38:12+00:00</published>
<link rel='alternate' type='text/html' href='https://git.toradex.cn/cgit/linux-toradex.git/commit/?id=92b80eb33c52583b48ed289bd993579b87b52d9a'/>
<id>92b80eb33c52583b48ed289bd993579b87b52d9a</id>
<content type='text'>
The module_put() function tests whether its argument is NULL and then
returns immediately. Thus the test around the call is not needed.

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring &lt;elfring@users.sourceforge.net&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
The module_put() function tests whether its argument is NULL and then
returns immediately. Thus the test around the call is not needed.

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring &lt;elfring@users.sourceforge.net&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>netlink: add API to retrieve all group memberships</title>
<updated>2015-06-21T17:18:18+00:00</updated>
<author>
<name>David Herrmann</name>
<email>dh.herrmann@gmail.com</email>
</author>
<published>2015-06-17T15:14:33+00:00</published>
<link rel='alternate' type='text/html' href='https://git.toradex.cn/cgit/linux-toradex.git/commit/?id=b42be38b2778eda2237fc759e55e3b698b05b315'/>
<id>b42be38b2778eda2237fc759e55e3b698b05b315</id>
<content type='text'>
This patch adds getsockopt(SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS) to
retrieve all groups a socket is a member of. Currently, we have to use
getsockname() and look at the nl.nl_groups bitmask. However, this mask is
limited to 32 groups. Hence, similar to NETLINK_ADD_MEMBERSHIP and
NETLINK_DROP_MEMBERSHIP, this adds a separate sockopt to manager higher
groups IDs than 32.

This new NETLINK_LIST_MEMBERSHIPS option takes a pointer to __u32 and the
size of the array. The array is filled with the full membership-set of the
socket, and the required array size is returned in optlen. Hence,
user-space can retry with a properly sized array in case it was too small.

Signed-off-by: David Herrmann &lt;dh.herrmann@gmail.com&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
This patch adds getsockopt(SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS) to
retrieve all groups a socket is a member of. Currently, we have to use
getsockname() and look at the nl.nl_groups bitmask. However, this mask is
limited to 32 groups. Hence, similar to NETLINK_ADD_MEMBERSHIP and
NETLINK_DROP_MEMBERSHIP, this adds a separate sockopt to manager higher
groups IDs than 32.

This new NETLINK_LIST_MEMBERSHIPS option takes a pointer to __u32 and the
size of the array. The array is filled with the full membership-set of the
socket, and the required array size is returned in optlen. Hence,
user-space can retry with a properly sized array in case it was too small.

Signed-off-by: David Herrmann &lt;dh.herrmann@gmail.com&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</pre>
</div>
</content>
</entry>
</feed>
