2005-04-16 15:20:36 -07:00
|
|
|
/* scm.c - Socket level control messages processing.
|
|
|
|
*
|
|
|
|
* Author: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
|
|
|
* Alignment and value checking mods by Craig Metz
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/signal.h>
|
2006-01-11 13:17:47 -07:00
|
|
|
#include <linux/capability.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/stat.h>
|
|
|
|
#include <linux/socket.h>
|
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/fcntl.h>
|
|
|
|
#include <linux/net.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/security.h>
|
2007-10-18 23:40:14 -07:00
|
|
|
#include <linux/pid.h>
|
|
|
|
#include <linux/nsproxy.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
#include <asm/system.h>
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
|
|
|
|
#include <net/protocol.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <net/sock.h>
|
|
|
|
#include <net/compat.h>
|
|
|
|
#include <net/scm.h>
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2007-02-09 07:24:36 -07:00
|
|
|
* Only allow a user to send credentials, that they could set with
|
2005-04-16 15:20:36 -07:00
|
|
|
* setu(g)id.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static __inline__ int scm_check_creds(struct ucred *creds)
|
|
|
|
{
|
2007-10-18 23:40:14 -07:00
|
|
|
if ((creds->pid == task_tgid_vnr(current) || capable(CAP_SYS_ADMIN)) &&
|
2005-04-16 15:20:36 -07:00
|
|
|
((creds->uid == current->uid || creds->uid == current->euid ||
|
|
|
|
creds->uid == current->suid) || capable(CAP_SETUID)) &&
|
|
|
|
((creds->gid == current->gid || creds->gid == current->egid ||
|
|
|
|
creds->gid == current->sgid) || capable(CAP_SETGID))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
|
|
|
|
{
|
|
|
|
int *fdp = (int*)CMSG_DATA(cmsg);
|
|
|
|
struct scm_fp_list *fpl = *fplp;
|
|
|
|
struct file **fpp;
|
|
|
|
int i, num;
|
|
|
|
|
|
|
|
num = (cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)))/sizeof(int);
|
|
|
|
|
|
|
|
if (num <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (num > SCM_MAX_FD)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!fpl)
|
|
|
|
{
|
|
|
|
fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL);
|
|
|
|
if (!fpl)
|
|
|
|
return -ENOMEM;
|
|
|
|
*fplp = fpl;
|
|
|
|
fpl->count = 0;
|
|
|
|
}
|
|
|
|
fpp = &fpl->fp[fpl->count];
|
|
|
|
|
|
|
|
if (fpl->count + num > SCM_MAX_FD)
|
|
|
|
return -EINVAL;
|
2007-02-09 07:24:36 -07:00
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
* Verify the descriptors and increment the usage count.
|
|
|
|
*/
|
2007-02-09 07:24:36 -07:00
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
for (i=0; i< num; i++)
|
|
|
|
{
|
|
|
|
int fd = fdp[i];
|
|
|
|
struct file *file;
|
|
|
|
|
|
|
|
if (fd < 0 || !(file = fget(fd)))
|
|
|
|
return -EBADF;
|
|
|
|
*fpp++ = file;
|
|
|
|
fpl->count++;
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __scm_destroy(struct scm_cookie *scm)
|
|
|
|
{
|
|
|
|
struct scm_fp_list *fpl = scm->fp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (fpl) {
|
|
|
|
scm->fp = NULL;
|
|
|
|
for (i=fpl->count-1; i>=0; i--)
|
|
|
|
fput(fpl->fp[i]);
|
|
|
|
kfree(fpl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
|
|
|
|
{
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
|
|
|
|
{
|
|
|
|
err = -EINVAL;
|
|
|
|
|
|
|
|
/* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
|
|
|
|
/* The first check was omitted in <= 2.2.5. The reasoning was
|
|
|
|
that parser checks cmsg_len in any case, so that
|
|
|
|
additional check would be work duplication.
|
2007-02-09 07:24:36 -07:00
|
|
|
But if cmsg_level is not SOL_SOCKET, we do not check
|
2005-04-16 15:20:36 -07:00
|
|
|
for too short ancillary data object at all! Oops.
|
|
|
|
OK, let's add it...
|
|
|
|
*/
|
|
|
|
if (!CMSG_OK(msg, cmsg))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (cmsg->cmsg_level != SOL_SOCKET)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (cmsg->cmsg_type)
|
|
|
|
{
|
|
|
|
case SCM_RIGHTS:
|
|
|
|
err=scm_fp_copy(cmsg, &p->fp);
|
|
|
|
if (err<0)
|
|
|
|
goto error;
|
|
|
|
break;
|
|
|
|
case SCM_CREDENTIALS:
|
|
|
|
if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
|
|
|
|
goto error;
|
|
|
|
memcpy(&p->creds, CMSG_DATA(cmsg), sizeof(struct ucred));
|
|
|
|
err = scm_check_creds(&p->creds);
|
|
|
|
if (err)
|
|
|
|
goto error;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->fp && !p->fp->count)
|
|
|
|
{
|
|
|
|
kfree(p->fp);
|
|
|
|
p->fp = NULL;
|
|
|
|
}
|
|
|
|
return 0;
|
2007-02-09 07:24:36 -07:00
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
error:
|
|
|
|
scm_destroy(p);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
|
|
|
|
{
|
2007-10-09 01:59:42 -07:00
|
|
|
struct cmsghdr __user *cm
|
|
|
|
= (__force struct cmsghdr __user *)msg->msg_control;
|
2005-04-16 15:20:36 -07:00
|
|
|
struct cmsghdr cmhdr;
|
|
|
|
int cmlen = CMSG_LEN(len);
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (MSG_CMSG_COMPAT & msg->msg_flags)
|
|
|
|
return put_cmsg_compat(msg, level, type, len, data);
|
|
|
|
|
|
|
|
if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
|
|
|
|
msg->msg_flags |= MSG_CTRUNC;
|
|
|
|
return 0; /* XXX: return error? check spec. */
|
|
|
|
}
|
|
|
|
if (msg->msg_controllen < cmlen) {
|
|
|
|
msg->msg_flags |= MSG_CTRUNC;
|
|
|
|
cmlen = msg->msg_controllen;
|
|
|
|
}
|
|
|
|
cmhdr.cmsg_level = level;
|
|
|
|
cmhdr.cmsg_type = type;
|
|
|
|
cmhdr.cmsg_len = cmlen;
|
|
|
|
|
|
|
|
err = -EFAULT;
|
|
|
|
if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
|
2007-02-09 07:24:36 -07:00
|
|
|
goto out;
|
2005-04-16 15:20:36 -07:00
|
|
|
if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
|
|
|
|
goto out;
|
|
|
|
cmlen = CMSG_SPACE(len);
|
[NET]: Fix function put_cmsg() which may cause usr application memory overflow
When used function put_cmsg() to copy kernel information to user
application memory, if the memory length given by user application is
not enough, by the bad length calculate of msg.msg_controllen,
put_cmsg() function may cause the msg.msg_controllen to be a large
value, such as 0xFFFFFFF0, so the following put_cmsg() can also write
data to usr application memory even usr has no valid memory to store
this. This may cause usr application memory overflow.
int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
{
struct cmsghdr __user *cm
= (__force struct cmsghdr __user *)msg->msg_control;
struct cmsghdr cmhdr;
int cmlen = CMSG_LEN(len);
~~~~~~~~~~~~~~~~~~~~~
int err;
if (MSG_CMSG_COMPAT & msg->msg_flags)
return put_cmsg_compat(msg, level, type, len, data);
if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
msg->msg_flags |= MSG_CTRUNC;
return 0; /* XXX: return error? check spec. */
}
if (msg->msg_controllen < cmlen) {
~~~~~~~~~~~~~~~~~~~~~~~~
msg->msg_flags |= MSG_CTRUNC;
cmlen = msg->msg_controllen;
}
cmhdr.cmsg_level = level;
cmhdr.cmsg_type = type;
cmhdr.cmsg_len = cmlen;
err = -EFAULT;
if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
goto out;
if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
goto out;
cmlen = CMSG_SPACE(len);
~~~~~~~~~~~~~~~~~~~~~~~~~~~
If MSG_CTRUNC flags is set, msg->msg_controllen is less than
CMSG_SPACE(len), "msg->msg_controllen -= cmlen" will cause unsinged int
type msg->msg_controllen to be a large value.
~~~~~~~~~~~~~~~~~~~~~~~~~~~
msg->msg_control += cmlen;
msg->msg_controllen -= cmlen;
~~~~~~~~~~~~~~~~~~~~~
err = 0;
out:
return err;
}
The same promble exists in put_cmsg_compat(). This patch can fix this
problem.
Signed-off-by: Wei Yongjun <yjwei@cn.fujitsu.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2007-12-20 15:36:44 -07:00
|
|
|
if (msg->msg_controllen < cmlen)
|
|
|
|
cmlen = msg->msg_controllen;
|
2005-04-16 15:20:36 -07:00
|
|
|
msg->msg_control += cmlen;
|
|
|
|
msg->msg_controllen -= cmlen;
|
|
|
|
err = 0;
|
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
|
|
|
|
{
|
2007-10-09 01:59:42 -07:00
|
|
|
struct cmsghdr __user *cm
|
|
|
|
= (__force struct cmsghdr __user*)msg->msg_control;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
int fdmax = 0;
|
|
|
|
int fdnum = scm->fp->count;
|
|
|
|
struct file **fp = scm->fp->fp;
|
|
|
|
int __user *cmfptr;
|
|
|
|
int err = 0, i;
|
|
|
|
|
|
|
|
if (MSG_CMSG_COMPAT & msg->msg_flags) {
|
|
|
|
scm_detach_fds_compat(msg, scm);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->msg_controllen > sizeof(struct cmsghdr))
|
|
|
|
fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr))
|
|
|
|
/ sizeof(int));
|
|
|
|
|
|
|
|
if (fdnum < fdmax)
|
|
|
|
fdmax = fdnum;
|
|
|
|
|
2007-10-09 01:59:42 -07:00
|
|
|
for (i=0, cmfptr=(__force int __user *)CMSG_DATA(cm); i<fdmax;
|
|
|
|
i++, cmfptr++)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
int new_fd;
|
|
|
|
err = security_file_receive(fp[i]);
|
|
|
|
if (err)
|
|
|
|
break;
|
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>
2007-07-15 23:40:34 -07:00
|
|
|
err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & msg->msg_flags
|
|
|
|
? O_CLOEXEC : 0);
|
2005-04-16 15:20:36 -07:00
|
|
|
if (err < 0)
|
|
|
|
break;
|
|
|
|
new_fd = err;
|
|
|
|
err = put_user(new_fd, cmfptr);
|
|
|
|
if (err) {
|
|
|
|
put_unused_fd(new_fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Bump the usage count and install the file. */
|
|
|
|
get_file(fp[i]);
|
|
|
|
fd_install(new_fd, fp[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
{
|
|
|
|
int cmlen = CMSG_LEN(i*sizeof(int));
|
2006-10-09 21:42:14 -07:00
|
|
|
err = put_user(SOL_SOCKET, &cm->cmsg_level);
|
2005-04-16 15:20:36 -07:00
|
|
|
if (!err)
|
|
|
|
err = put_user(SCM_RIGHTS, &cm->cmsg_type);
|
|
|
|
if (!err)
|
|
|
|
err = put_user(cmlen, &cm->cmsg_len);
|
|
|
|
if (!err) {
|
|
|
|
cmlen = CMSG_SPACE(i*sizeof(int));
|
|
|
|
msg->msg_control += cmlen;
|
|
|
|
msg->msg_controllen -= cmlen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i < fdnum || (fdnum && fdmax <= 0))
|
|
|
|
msg->msg_flags |= MSG_CTRUNC;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All of the files that fit in the message have had their
|
|
|
|
* usage counts incremented, so we just free the list.
|
|
|
|
*/
|
|
|
|
__scm_destroy(scm);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
|
|
|
|
{
|
|
|
|
struct scm_fp_list *new_fpl;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!fpl)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
new_fpl = kmalloc(sizeof(*fpl), GFP_KERNEL);
|
|
|
|
if (new_fpl) {
|
|
|
|
for (i=fpl->count-1; i>=0; i--)
|
|
|
|
get_file(fpl->fp[i]);
|
|
|
|
memcpy(new_fpl, fpl, sizeof(*fpl));
|
|
|
|
}
|
|
|
|
return new_fpl;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(__scm_destroy);
|
|
|
|
EXPORT_SYMBOL(__scm_send);
|
|
|
|
EXPORT_SYMBOL(put_cmsg);
|
|
|
|
EXPORT_SYMBOL(scm_detach_fds);
|
|
|
|
EXPORT_SYMBOL(scm_fp_dup);
|