Skip to contentSkip to conversation

af_unix: fix unix_nr_socks check in unix_create1()

PATCH In review Xi Wang 2 participants 3 messages Aug 25, 2012

Subsystems (3)
lore
Share & tools
lore

Xi Wang

author
#0 lore raw

PATCH 1/1 af_unix: fix unix_nr_socks check in unix_create1()

This patch complements 518de9b3 ("fs: allow for more than 2^31 files").

get_max_files() returns files_stat.max_files, which can be set to any value from user space via /proc/sys/fs/file-max. A large file-max will cause an integer overflow in the following check:

	if (atomic_long_read(&unix_nr_socks) > 2 * get_max_files())
		goto out;

The kernel will then fail to create a unix domain socket.

Rewrite the check using "/ 2" on the left-hand side.

Cc Eric Dumazet <[email protected]>
---
 net/unix/af_unix.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/unix/af_unix.c +2 -2
index c5ee4ff..99a8bc9 100644
@@ -630,9 +630,9 @@ static struct sock *unix_create1(struct net *net, struct socket *sock)
 {
 	struct sock *sk = NULL;
 	struct unix_sock *u;
+	unsigned long nr_socks = atomic_long_inc_return(&unix_nr_socks);
 
-	atomic_long_inc(&unix_nr_socks);
-	if (atomic_long_read(&unix_nr_socks) > 2 * get_max_files())
+	if (nr_socks / 2 > get_max_files())
 		goto out;
 
 	sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_proto);
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 25 lines · Xi Wang

This patch complements 518de9b3 ("fs: allow for more than 2^31 files").

get_max_files() returns files_stat.max_files, which can be set to any value from user space via /proc/sys/fs/file-max. A large file-max will cause an integer overflow in the following check:

	if (atomic_long_read(&unix_nr_socks) > 2 * get_max_files())
		goto out;

The kernel will then fail to create a unix domain socket.

Rewrite the check using "/ 2" on the left-hand side.

Cc Eric Dumazet <[email protected]>
---
 net/unix/af_unix.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/unix/af_unix.c +0 -0
index c5ee4ff..99a8bc9 100644
@@ -630,9 +630,9 @@ static struct sock *unix_create1(struct net *net, struct socket *sock)
 {

net/unix/af_unix.c static struct sock *unix_create1(struct net *net, struct socket *sock) · line 638

Quoted 10 lines
net/unix/af_unix.c +2 -2
 	struct sock *sk = NULL;
 	struct unix_sock *u;
+	unsigned long nr_socks = atomic_long_inc_return(&unix_nr_socks);
 
-	atomic_long_inc(&unix_nr_socks);
-	if (atomic_long_read(&unix_nr_socks) > 2 * get_max_files())
+	if (nr_socks / 2 > get_max_files())
 		goto out;
 
 	sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_proto);

I dont think this patch is right.

atomic_long_inc_return() is expensive, more than atomic_long_inc()

And setting a 2^63 limit for max number of files is plain wrong, as there is no way a kernel can allocate 2^63 files structures.

(same apply on 32bit arches, but for 2^31)

If you feel you must warn a sysadmin of stupid settings, add sane boundaries in kernel/sysctl.c, and send your patch to lkml instead.

author
#2 lore raw
Quoted 11 lines · Eric Dumazet

I dont think this patch is right.

atomic_long_inc_return() is expensive, more than atomic_long_inc()

And setting a 2^63 limit for max number of files is plain wrong, as there is no way a kernel can allocate 2^63 files structures.

(same apply on 32bit arches, but for 2^31)

If you feel you must warn a sysadmin of stupid settings, add sane boundaries in kernel/sysctl.c, and send your patch to lkml instead.

Yeah, I agree it's better to limit max_files in sysctl. I will send another patch instead. Thanks.

- xi