RE: [PATCH v1 1/1] usbip: deletion of incorrect socket descriptor checking

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

> If fd >= FD_SETSIZE then you can't use select.

In BUG section in man select:

POSIX allows an implementation to define an upper limit, advertised
via the constant FD_SETSIZE, on the range of file descriptors that
can be specified in a file descriptor set.  The Linux kernel imposes
no fixed limit, but the glibc implementation makes fd_set a fixed-
size type, with FD_SETSIZE defined as 1024, and the FD_*() macros
operating according to that limit.  To monitor file descriptors
greater than 1023, use poll(2) instead.

USB/IP uses poll().

tools/usb/usbip/src/usbipd.c#L567.
http://lxr.free-electrons.com/source/tools/usb/usbip/src/usbipd.c#L567
r = ppoll(fds, nsockfd, &timeout, &sigmask);
Here, nsockfd is 2 or 1 (listen sockaddr IPv4 and/or IPv6).

Also I tested with following code.

---
diff -uprN -X ./dontdiff_exc_linux linux-4.8-rc9-33-nofile/tools/usb/usb
ip/src/usbipd.c linux-4.8-rc9-33-nofile-test/tools/usb/usbip/src/usbipd.
c
--- linux-4.8-rc9-33-nofile/tools/usb/usbip/src/usbipd.c        2016-09-
29 19:04:28.761410447 +0900
+++ linux-4.8-rc9-33-nofile-test/tools/usb/usbip/src/usbipd.c   2016-10-
03 10:55:53.120517533 +0900
@@ -183,6 +183,28 @@ static void addrinfo_to_text(struct addr
        snprintf(buf, buf_size, "%s:%s", hbuf, sbuf);
 }

+FILE *tmps[FD_SETSIZE];
+
+static void open_test_files(void)
+{
+       int i;
+
+       for (i = 0; i < FD_SETSIZE; i++) {
+               tmps[i] = tmpfile();
+               if (tmps[i] == NULL)
+                       err("alloc tmp");
+       }
+}
+
+static void close_test_files(void)
+{
+       int i;
+
+       for (i = 0; i < FD_SETSIZE; i++) {
+               fclose(tmps[i]);
+       }
+}
+
 static int listen_all_addrinfo(struct addrinfo *ai_head, int sockfdlist[],
                             int maxsockfd)
 {
@@ -191,6 +213,8 @@ static int listen_all_addrinfo(struct ad
        const size_t ai_buf_size = NI_MAXHOST + NI_MAXSERV + 2;
        char ai_buf[ai_buf_size];

+       open_test_files();
+
        for (ai = ai_head; ai && nsockfd < maxsockfd; ai = ai->ai_next) {
                int sock;
+static void close_test_files(void)
+{
+       int i;
+
+       for (i = 0; i < FD_SETSIZE; i++) {
+               fclose(tmps[i]);
+       }
+}
+
static int listen_all_addrinfo(struct addrinfo *ai_head, int sockfdlist[],
                             int maxsockfd)
 {
@@ -191,6 +213,8 @@ static int listen_all_addrinfo(struct ad
        const size_t ai_buf_size = NI_MAXHOST + NI_MAXSERV + 2;
        char ai_buf[ai_buf_size];

+       open_test_files();
+
        for (ai = ai_head; ai && nsockfd < maxsockfd; ai = ai->ai_next) {
                int sock;

Settings in /etc/security/limits.conf
---
root soft nofile 65536
root hard nofile 65536

$ cat /proc/PID/limits
---
Max open files            65536                65536                files

USB/IP log
---
usbip: debug: usbipd.c:222:[listen_all_addrinfo] opening 0.0.0.0:3240
usbip: info: socket 1027 : 1024
usbip: debug: usbip_network.c:253:[usbip_net_set_v6only] setsockopt: IPV
6_V6ONLY
usbip: info: listening on 0.0.0.0:3240
usbip: debug: usbipd.c:222:[listen_all_addrinfo] opening :::3240
usbip: info: socket 1028 : 1024
usbip: info: listening on :::3240

Best Regards,

n.iwata
//
> -----Original Message-----
> From: David Laight [mailto:David.Laight@xxxxxxxxxx]
> Sent: Friday, September 30, 2016 8:08 PM
> To: fx IWATA NOBUO; valentina.manea.m@xxxxxxxxx; shuah.kh@xxxxxxxxxxx;
> gregkh@xxxxxxxxxxxxxxxxxxx; linux-usb@xxxxxxxxxxxxxxx
> Cc: fx MICHIMURA TADAO
> Subject: RE: [PATCH v1 1/1] usbip: deletion of incorrect socket descriptor
> checking
> 
> From: Nobuo Iwata
> > Sent: 30 September 2016 06:44
> > This patch removes checking of socket descriptor value in daemons.
> >
> > It was checked to be less than FD_SETSIZE(1024 usually) but it's not
> > correct.
> >
> > To be exact, the maximum value of descriptor comes from
> > rlimit(RLIMIT_NOFILE).
> >
> > Following kernel code determines the value :
> >     get_unused_fd_flags() : fs/files.c
> >     __alloc_fd() : fs/files.c
> >     expand_files() : fs/files.c
> >
> > The defalut (soft limit) is defines as INR_OPEN_CUR(1024) in
> > include/linux/fs.h which is referenced form INIT_RLIMS in
> > include/asm-generic/resource.h. The value may be modified with ulimt,
> > sysctl, security configuration and etc.
> >
> > With the kernel code above, when socket() system call returns positive
> > value, the value must be within rlimit(RLIMIT_NOFILE). No extra
> > checking is needed when socket() returns positive.
> 
> I'm guessing that the problem is that the code wants to use select() on
> the socket.
> The value of RLIMIT_NOFILE isn't really relevant.
> If fd >= FD_SETSIZE then you can't use select.
> 
> In reality any program that has anywhere near that number of open fd would
> be better off using something other than select().
> 
> 	David

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux