diff options
author | Ulrich Drepper <drepper@redhat.com> | 2007-07-16 02:40:34 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2007-07-16 12:05:45 -0400 |
commit | 4a19542e5f694cd408a32c3d9dc593ba9366e2d7 (patch) | |
tree | 12f5fd603b516b4e24ec4850d5589273d24be569 | |
parent | f23513e8d96cf5e6cf8d2ff0cb5dd6bbc33995e4 (diff) |
O_CLOEXEC for SCM_RIGHTS
Part two in the O_CLOEXEC saga: adding support for file descriptors received
through Unix domain sockets.
The patch is once again pretty minimal, it introduces a new flag for recvmsg
and passes it just like the existing MSG_CMSG_COMPAT flag. I think this bit
is not used otherwise but the networking people will know better.
This new flag is not recognized by recvfrom and recv. These functions cannot
be used for that purpose and the asymmetry this introduces is not worse than
the already existing MSG_CMSG_COMPAT situations.
The patch must be applied on the patch which introduced O_CLOEXEC. It has to
remove static from the new get_unused_fd_flags function but since scm.c cannot
live in a module the function still hasn't to be exported.
Here's a test program to make sure the code works. It's so much longer than
the actual patch...
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#ifndef O_CLOEXEC
# define O_CLOEXEC 02000000
#endif
#ifndef MSG_CMSG_CLOEXEC
# define MSG_CMSG_CLOEXEC 0x40000000
#endif
int
main (int argc, char *argv[])
{
if (argc > 1)
{
int fd = atol (argv[1]);
printf ("child: fd = %d\n", fd);
if (fcntl (fd, F_GETFD) == 0 || errno != EBADF)
{
puts ("file descriptor valid in child");
return 1;
}
return 0;
}
struct sockaddr_un sun;
strcpy (sun.sun_path, "./testsocket");
sun.sun_family = AF_UNIX;
char databuf[] = "hello";
struct iovec iov[1];
iov[0].iov_base = databuf;
iov[0].iov_len = sizeof (databuf);
union
{
struct cmsghdr hdr;
char bytes[CMSG_SPACE (sizeof (int))];
} buf;
struct msghdr msg = { .msg_iov = iov, .msg_iovlen = 1,
.msg_control = buf.bytes,
.msg_controllen = sizeof (buf) };
struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN (sizeof (int));
msg.msg_controllen = cmsg->cmsg_len;
pid_t child = fork ();
if (child == -1)
error (1, errno, "fork");
if (child == 0)
{
int sock = socket (PF_UNIX, SOCK_STREAM, 0);
if (sock < 0)
error (1, errno, "socket");
if (bind (sock, (struct sockaddr *) &sun, sizeof (sun)) < 0)
error (1, errno, "bind");
if (listen (sock, SOMAXCONN) < 0)
error (1, errno, "listen");
int conn = accept (sock, NULL, NULL);
if (conn == -1)
error (1, errno, "accept");
*(int *) CMSG_DATA (cmsg) = sock;
if (sendmsg (conn, &msg, MSG_NOSIGNAL) < 0)
error (1, errno, "sendmsg");
return 0;
}
/* For a test suite this should be more robust like a
barrier in shared memory. */
sleep (1);
int sock = socket (PF_UNIX, SOCK_STREAM, 0);
if (sock < 0)
error (1, errno, "socket");
if (connect (sock, (struct sockaddr *) &sun, sizeof (sun)) < 0)
error (1, errno, "connect");
unlink (sun.sun_path);
*(int *) CMSG_DATA (cmsg) = -1;
if (recvmsg (sock, &msg, MSG_CMSG_CLOEXEC) < 0)
error (1, errno, "recvmsg");
int fd = *(int *) CMSG_DATA (cmsg);
if (fd == -1)
error (1, 0, "no descriptor received");
char fdname[20];
snprintf (fdname, sizeof (fdname), "%d", fd);
execl ("/proc/self/exe", argv[0], fdname, NULL);
puts ("execl failed");
return 1;
}
[akpm@linux-foundation.org: Fix fastcall inconsistency noted by Michael Buesch]
[akpm@linux-foundation.org: build fix]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Michael Buesch <mb@bu3sch.de>
Cc: Michael Kerrisk <mtk-manpages@gmx.net>
Acked-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | fs/open.c | 2 | ||||
-rw-r--r-- | include/linux/file.h | 1 | ||||
-rw-r--r-- | include/linux/socket.h | 3 | ||||
-rw-r--r-- | net/compat.c | 3 | ||||
-rw-r--r-- | net/core/scm.c | 3 | ||||
-rw-r--r-- | net/socket.c | 4 |
6 files changed, 10 insertions, 6 deletions
@@ -855,7 +855,7 @@ EXPORT_SYMBOL(dentry_open); | |||
855 | /* | 855 | /* |
856 | * Find an empty file descriptor entry, and mark it busy. | 856 | * Find an empty file descriptor entry, and mark it busy. |
857 | */ | 857 | */ |
858 | static int get_unused_fd_flags(int flags) | 858 | int get_unused_fd_flags(int flags) |
859 | { | 859 | { |
860 | struct files_struct * files = current->files; | 860 | struct files_struct * files = current->files; |
861 | int fd, error; | 861 | int fd, error; |
diff --git a/include/linux/file.h b/include/linux/file.h index a59001e9ea58..0114fbc78061 100644 --- a/include/linux/file.h +++ b/include/linux/file.h | |||
@@ -73,6 +73,7 @@ extern struct file * FASTCALL(fget_light(unsigned int fd, int *fput_needed)); | |||
73 | extern void FASTCALL(set_close_on_exec(unsigned int fd, int flag)); | 73 | extern void FASTCALL(set_close_on_exec(unsigned int fd, int flag)); |
74 | extern void put_filp(struct file *); | 74 | extern void put_filp(struct file *); |
75 | extern int get_unused_fd(void); | 75 | extern int get_unused_fd(void); |
76 | extern int get_unused_fd_flags(int flags); | ||
76 | extern void FASTCALL(put_unused_fd(unsigned int fd)); | 77 | extern void FASTCALL(put_unused_fd(unsigned int fd)); |
77 | struct kmem_cache; | 78 | struct kmem_cache; |
78 | 79 | ||
diff --git a/include/linux/socket.h b/include/linux/socket.h index fe195c97a89d..f852e1afd65a 100644 --- a/include/linux/socket.h +++ b/include/linux/socket.h | |||
@@ -253,6 +253,9 @@ struct ucred { | |||
253 | 253 | ||
254 | #define MSG_EOF MSG_FIN | 254 | #define MSG_EOF MSG_FIN |
255 | 255 | ||
256 | #define MSG_CMSG_CLOEXEC 0x40000000 /* Set close_on_exit for file | ||
257 | descriptor received through | ||
258 | SCM_RIGHTS */ | ||
256 | #if defined(CONFIG_COMPAT) | 259 | #if defined(CONFIG_COMPAT) |
257 | #define MSG_CMSG_COMPAT 0x80000000 /* This message needs 32 bit fixups */ | 260 | #define MSG_CMSG_COMPAT 0x80000000 /* This message needs 32 bit fixups */ |
258 | #else | 261 | #else |
diff --git a/net/compat.c b/net/compat.c index 9a0f5f2b90c8..d74d82155d78 100644 --- a/net/compat.c +++ b/net/compat.c | |||
@@ -276,7 +276,8 @@ void scm_detach_fds_compat(struct msghdr *kmsg, struct scm_cookie *scm) | |||
276 | err = security_file_receive(fp[i]); | 276 | err = security_file_receive(fp[i]); |
277 | if (err) | 277 | if (err) |
278 | break; | 278 | break; |
279 | err = get_unused_fd(); | 279 | err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & kmsg->msg_flags |
280 | ? O_CLOEXEC : 0); | ||
280 | if (err < 0) | 281 | if (err < 0) |
281 | break; | 282 | break; |
282 | new_fd = err; | 283 | new_fd = err; |
diff --git a/net/core/scm.c b/net/core/scm.c index 292ad8d5ad76..44c4ec2c8769 100644 --- a/net/core/scm.c +++ b/net/core/scm.c | |||
@@ -228,7 +228,8 @@ void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm) | |||
228 | err = security_file_receive(fp[i]); | 228 | err = security_file_receive(fp[i]); |
229 | if (err) | 229 | if (err) |
230 | break; | 230 | break; |
231 | err = get_unused_fd(); | 231 | err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & msg->msg_flags |
232 | ? O_CLOEXEC : 0); | ||
232 | if (err < 0) | 233 | if (err < 0) |
233 | break; | 234 | break; |
234 | new_fd = err; | 235 | new_fd = err; |
diff --git a/net/socket.c b/net/socket.c index f4530196a70a..b71114250046 100644 --- a/net/socket.c +++ b/net/socket.c | |||
@@ -1939,9 +1939,7 @@ asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, | |||
1939 | total_len = err; | 1939 | total_len = err; |
1940 | 1940 | ||
1941 | cmsg_ptr = (unsigned long)msg_sys.msg_control; | 1941 | cmsg_ptr = (unsigned long)msg_sys.msg_control; |
1942 | msg_sys.msg_flags = 0; | 1942 | msg_sys.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT); |
1943 | if (MSG_CMSG_COMPAT & flags) | ||
1944 | msg_sys.msg_flags = MSG_CMSG_COMPAT; | ||
1945 | 1943 | ||
1946 | if (sock->file->f_flags & O_NONBLOCK) | 1944 | if (sock->file->f_flags & O_NONBLOCK) |
1947 | flags |= MSG_DONTWAIT; | 1945 | flags |= MSG_DONTWAIT; |