diff options
author | Ulrich Drepper <drepper@redhat.com> | 2008-11-19 18:36:14 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2008-11-19 21:49:57 -0500 |
commit | de11defebf00007677fb7ee91d9b089b78786fbb (patch) | |
tree | 8219faf1b7992be77a612e778c2573c55f56cf19 /net/socket.c | |
parent | cf7ee554f3a324e98181b0ea249d9d5be3a0acb8 (diff) |
reintroduce accept4
Introduce a new accept4() system call. The addition of this system call
matches analogous changes in 2.6.27 (dup3(), evenfd2(), signalfd4(),
inotify_init1(), epoll_create1(), pipe2()) which added new system calls
that differed from analogous traditional system calls in adding a flags
argument that can be used to access additional functionality.
The accept4() system call is exactly the same as accept(), except that
it adds a flags bit-mask argument. Two flags are initially implemented.
(Most of the new system calls in 2.6.27 also had both of these flags.)
SOCK_CLOEXEC causes the close-on-exec (FD_CLOEXEC) flag to be enabled
for the new file descriptor returned by accept4(). This is a useful
security feature to avoid leaking information in a multithreaded
program where one thread is doing an accept() at the same time as
another thread is doing a fork() plus exec(). More details here:
http://udrepper.livejournal.com/20407.html "Secure File Descriptor Handling",
Ulrich Drepper).
The other flag is SOCK_NONBLOCK, which causes the O_NONBLOCK flag
to be enabled on the new open file description created by accept4().
(This flag is merely a convenience, saving the use of additional calls
fcntl(F_GETFL) and fcntl (F_SETFL) to achieve the same result.
Here's a test program. Works on x86-32. Should work on x86-64, but
I (mtk) don't have a system to hand to test with.
It tests accept4() with each of the four possible combinations of
SOCK_CLOEXEC and SOCK_NONBLOCK set/clear in 'flags', and verifies
that the appropriate flags are set on the file descriptor/open file
description returned by accept4().
I tested Ulrich's patch in this thread by applying against 2.6.28-rc2,
and it passes according to my test program.
/* test_accept4.c
Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
<mtk.manpages@gmail.com>
Licensed under the GNU GPLv2 or later.
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#define PORT_NUM 33333
#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
/**********************************************************************/
/* The following is what we need until glibc gets a wrapper for
accept4() */
/* Flags for socket(), socketpair(), accept4() */
#ifndef SOCK_CLOEXEC
#define SOCK_CLOEXEC O_CLOEXEC
#endif
#ifndef SOCK_NONBLOCK
#define SOCK_NONBLOCK O_NONBLOCK
#endif
#ifdef __x86_64__
#define SYS_accept4 288
#elif __i386__
#define USE_SOCKETCALL 1
#define SYS_ACCEPT4 18
#else
#error "Sorry -- don't know the syscall # on this architecture"
#endif
static int
accept4(int fd, struct sockaddr *sockaddr, socklen_t *addrlen, int flags)
{
printf("Calling accept4(): flags = %x", flags);
if (flags != 0) {
printf(" (");
if (flags & SOCK_CLOEXEC)
printf("SOCK_CLOEXEC");
if ((flags & SOCK_CLOEXEC) && (flags & SOCK_NONBLOCK))
printf(" ");
if (flags & SOCK_NONBLOCK)
printf("SOCK_NONBLOCK");
printf(")");
}
printf("\n");
#if USE_SOCKETCALL
long args[6];
args[0] = fd;
args[1] = (long) sockaddr;
args[2] = (long) addrlen;
args[3] = flags;
return syscall(SYS_socketcall, SYS_ACCEPT4, args);
#else
return syscall(SYS_accept4, fd, sockaddr, addrlen, flags);
#endif
}
/**********************************************************************/
static int
do_test(int lfd, struct sockaddr_in *conn_addr,
int closeonexec_flag, int nonblock_flag)
{
int connfd, acceptfd;
int fdf, flf, fdf_pass, flf_pass;
struct sockaddr_in claddr;
socklen_t addrlen;
printf("=======================================\n");
connfd = socket(AF_INET, SOCK_STREAM, 0);
if (connfd == -1)
die("socket");
if (connect(connfd, (struct sockaddr *) conn_addr,
sizeof(struct sockaddr_in)) == -1)
die("connect");
addrlen = sizeof(struct sockaddr_in);
acceptfd = accept4(lfd, (struct sockaddr *) &claddr, &addrlen,
closeonexec_flag | nonblock_flag);
if (acceptfd == -1) {
perror("accept4()");
close(connfd);
return 0;
}
fdf = fcntl(acceptfd, F_GETFD);
if (fdf == -1)
die("fcntl:F_GETFD");
fdf_pass = ((fdf & FD_CLOEXEC) != 0) ==
((closeonexec_flag & SOCK_CLOEXEC) != 0);
printf("Close-on-exec flag is %sset (%s); ",
(fdf & FD_CLOEXEC) ? "" : "not ",
fdf_pass ? "OK" : "failed");
flf = fcntl(acceptfd, F_GETFL);
if (flf == -1)
die("fcntl:F_GETFD");
flf_pass = ((flf & O_NONBLOCK) != 0) ==
((nonblock_flag & SOCK_NONBLOCK) !=0);
printf("nonblock flag is %sset (%s)\n",
(flf & O_NONBLOCK) ? "" : "not ",
flf_pass ? "OK" : "failed");
close(acceptfd);
close(connfd);
printf("Test result: %s\n", (fdf_pass && flf_pass) ? "PASS" : "FAIL");
return fdf_pass && flf_pass;
}
static int
create_listening_socket(int port_num)
{
struct sockaddr_in svaddr;
int lfd;
int optval;
memset(&svaddr, 0, sizeof(struct sockaddr_in));
svaddr.sin_family = AF_INET;
svaddr.sin_addr.s_addr = htonl(INADDR_ANY);
svaddr.sin_port = htons(port_num);
lfd = socket(AF_INET, SOCK_STREAM, 0);
if (lfd == -1)
die("socket");
optval = 1;
if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval)) == -1)
die("setsockopt");
if (bind(lfd, (struct sockaddr *) &svaddr,
sizeof(struct sockaddr_in)) == -1)
die("bind");
if (listen(lfd, 5) == -1)
die("listen");
return lfd;
}
int
main(int argc, char *argv[])
{
struct sockaddr_in conn_addr;
int lfd;
int port_num;
int passed;
passed = 1;
port_num = (argc > 1) ? atoi(argv[1]) : PORT_NUM;
memset(&conn_addr, 0, sizeof(struct sockaddr_in));
conn_addr.sin_family = AF_INET;
conn_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
conn_addr.sin_port = htons(port_num);
lfd = create_listening_socket(port_num);
if (!do_test(lfd, &conn_addr, 0, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, 0, SOCK_NONBLOCK))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, SOCK_NONBLOCK))
passed = 0;
close(lfd);
exit(passed ? EXIT_SUCCESS : EXIT_FAILURE);
}
[mtk.manpages@gmail.com: rewrote changelog, updated test program]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Tested-by: Michael Kerrisk <mtk.manpages@gmail.com>
Acked-by: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: <linux-api@vger.kernel.org>
Cc: <linux-arch@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'net/socket.c')
-rw-r--r-- | net/socket.c | 80 |
1 files changed, 10 insertions, 70 deletions
diff --git a/net/socket.c b/net/socket.c index 57550c3bcabe..92764d836891 100644 --- a/net/socket.c +++ b/net/socket.c | |||
@@ -1426,8 +1426,8 @@ asmlinkage long sys_listen(int fd, int backlog) | |||
1426 | * clean when we restucture accept also. | 1426 | * clean when we restucture accept also. |
1427 | */ | 1427 | */ |
1428 | 1428 | ||
1429 | long do_accept(int fd, struct sockaddr __user *upeer_sockaddr, | 1429 | asmlinkage long sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr, |
1430 | int __user *upeer_addrlen, int flags) | 1430 | int __user *upeer_addrlen, int flags) |
1431 | { | 1431 | { |
1432 | struct socket *sock, *newsock; | 1432 | struct socket *sock, *newsock; |
1433 | struct file *newfile; | 1433 | struct file *newfile; |
@@ -1510,66 +1510,10 @@ out_fd: | |||
1510 | goto out_put; | 1510 | goto out_put; |
1511 | } | 1511 | } |
1512 | 1512 | ||
1513 | #if 0 | ||
1514 | #ifdef HAVE_SET_RESTORE_SIGMASK | ||
1515 | asmlinkage long sys_paccept(int fd, struct sockaddr __user *upeer_sockaddr, | ||
1516 | int __user *upeer_addrlen, | ||
1517 | const sigset_t __user *sigmask, | ||
1518 | size_t sigsetsize, int flags) | ||
1519 | { | ||
1520 | sigset_t ksigmask, sigsaved; | ||
1521 | int ret; | ||
1522 | |||
1523 | if (sigmask) { | ||
1524 | /* XXX: Don't preclude handling different sized sigset_t's. */ | ||
1525 | if (sigsetsize != sizeof(sigset_t)) | ||
1526 | return -EINVAL; | ||
1527 | if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask))) | ||
1528 | return -EFAULT; | ||
1529 | |||
1530 | sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP)); | ||
1531 | sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved); | ||
1532 | } | ||
1533 | |||
1534 | ret = do_accept(fd, upeer_sockaddr, upeer_addrlen, flags); | ||
1535 | |||
1536 | if (ret < 0 && signal_pending(current)) { | ||
1537 | /* | ||
1538 | * Don't restore the signal mask yet. Let do_signal() deliver | ||
1539 | * the signal on the way back to userspace, before the signal | ||
1540 | * mask is restored. | ||
1541 | */ | ||
1542 | if (sigmask) { | ||
1543 | memcpy(¤t->saved_sigmask, &sigsaved, | ||
1544 | sizeof(sigsaved)); | ||
1545 | set_restore_sigmask(); | ||
1546 | } | ||
1547 | } else if (sigmask) | ||
1548 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | ||
1549 | |||
1550 | return ret; | ||
1551 | } | ||
1552 | #else | ||
1553 | asmlinkage long sys_paccept(int fd, struct sockaddr __user *upeer_sockaddr, | ||
1554 | int __user *upeer_addrlen, | ||
1555 | const sigset_t __user *sigmask, | ||
1556 | size_t sigsetsize, int flags) | ||
1557 | { | ||
1558 | /* The platform does not support restoring the signal mask in the | ||
1559 | * return path. So we do not allow using paccept() with a signal | ||
1560 | * mask. */ | ||
1561 | if (sigmask) | ||
1562 | return -EINVAL; | ||
1563 | |||
1564 | return do_accept(fd, upeer_sockaddr, upeer_addrlen, flags); | ||
1565 | } | ||
1566 | #endif | ||
1567 | #endif | ||
1568 | |||
1569 | asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, | 1513 | asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, |
1570 | int __user *upeer_addrlen) | 1514 | int __user *upeer_addrlen) |
1571 | { | 1515 | { |
1572 | return do_accept(fd, upeer_sockaddr, upeer_addrlen, 0); | 1516 | return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0); |
1573 | } | 1517 | } |
1574 | 1518 | ||
1575 | /* | 1519 | /* |
@@ -2096,7 +2040,7 @@ static const unsigned char nargs[19]={ | |||
2096 | AL(0),AL(3),AL(3),AL(3),AL(2),AL(3), | 2040 | AL(0),AL(3),AL(3),AL(3),AL(2),AL(3), |
2097 | AL(3),AL(3),AL(4),AL(4),AL(4),AL(6), | 2041 | AL(3),AL(3),AL(4),AL(4),AL(4),AL(6), |
2098 | AL(6),AL(2),AL(5),AL(5),AL(3),AL(3), | 2042 | AL(6),AL(2),AL(5),AL(5),AL(3),AL(3), |
2099 | AL(6) | 2043 | AL(4) |
2100 | }; | 2044 | }; |
2101 | 2045 | ||
2102 | #undef AL | 2046 | #undef AL |
@@ -2115,7 +2059,7 @@ asmlinkage long sys_socketcall(int call, unsigned long __user *args) | |||
2115 | unsigned long a0, a1; | 2059 | unsigned long a0, a1; |
2116 | int err; | 2060 | int err; |
2117 | 2061 | ||
2118 | if (call < 1 || call > SYS_PACCEPT) | 2062 | if (call < 1 || call > SYS_ACCEPT4) |
2119 | return -EINVAL; | 2063 | return -EINVAL; |
2120 | 2064 | ||
2121 | /* copy_from_user should be SMP safe. */ | 2065 | /* copy_from_user should be SMP safe. */ |
@@ -2143,9 +2087,8 @@ asmlinkage long sys_socketcall(int call, unsigned long __user *args) | |||
2143 | err = sys_listen(a0, a1); | 2087 | err = sys_listen(a0, a1); |
2144 | break; | 2088 | break; |
2145 | case SYS_ACCEPT: | 2089 | case SYS_ACCEPT: |
2146 | err = | 2090 | err = sys_accept4(a0, (struct sockaddr __user *)a1, |
2147 | do_accept(a0, (struct sockaddr __user *)a1, | 2091 | (int __user *)a[2], 0); |
2148 | (int __user *)a[2], 0); | ||
2149 | break; | 2092 | break; |
2150 | case SYS_GETSOCKNAME: | 2093 | case SYS_GETSOCKNAME: |
2151 | err = | 2094 | err = |
@@ -2192,12 +2135,9 @@ asmlinkage long sys_socketcall(int call, unsigned long __user *args) | |||
2192 | case SYS_RECVMSG: | 2135 | case SYS_RECVMSG: |
2193 | err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]); | 2136 | err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]); |
2194 | break; | 2137 | break; |
2195 | case SYS_PACCEPT: | 2138 | case SYS_ACCEPT4: |
2196 | err = | 2139 | err = sys_accept4(a0, (struct sockaddr __user *)a1, |
2197 | sys_paccept(a0, (struct sockaddr __user *)a1, | 2140 | (int __user *)a[2], a[3]); |
2198 | (int __user *)a[2], | ||
2199 | (const sigset_t __user *) a[3], | ||
2200 | a[4], a[5]); | ||
2201 | break; | 2141 | break; |
2202 | default: | 2142 | default: |
2203 | err = -EINVAL; | 2143 | err = -EINVAL; |