OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [core/] [scm.c] - Diff between revs 1278 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 1278 Rev 1765
/* scm.c - Socket level control messages processing.
/* scm.c - Socket level control messages processing.
 *
 *
 * Author:      Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 * Author:      Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 *              Alignment and value checking mods by Craig Metz
 *              Alignment and value checking mods by Craig Metz
 *
 *
 *              This program is free software; you can redistribute it and/or
 *              This program is free software; you can redistribute it and/or
 *              modify it under the terms of the GNU General Public License
 *              modify it under the terms of the GNU General Public License
 *              as published by the Free Software Foundation; either version
 *              as published by the Free Software Foundation; either version
 *              2 of the License, or (at your option) any later version.
 *              2 of the License, or (at your option) any later version.
 */
 */
 
 
#include <linux/signal.h>
#include <linux/signal.h>
#include <linux/errno.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/mm.h>
#include <linux/kernel.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/major.h>
#include <linux/stat.h>
#include <linux/stat.h>
#include <linux/socket.h>
#include <linux/socket.h>
#include <linux/file.h>
#include <linux/file.h>
#include <linux/fcntl.h>
#include <linux/fcntl.h>
#include <linux/net.h>
#include <linux/net.h>
#include <linux/interrupt.h>
#include <linux/interrupt.h>
#include <linux/netdevice.h>
#include <linux/netdevice.h>
 
 
#include <asm/system.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <asm/uaccess.h>
 
 
#include <net/protocol.h>
#include <net/protocol.h>
#include <linux/skbuff.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include <net/sock.h>
#include <net/scm.h>
#include <net/scm.h>
 
 
 
 
/*
/*
 *      Only allow a user to send credentials, that they could set with
 *      Only allow a user to send credentials, that they could set with
 *      setu(g)id.
 *      setu(g)id.
 */
 */
 
 
static __inline__ int scm_check_creds(struct ucred *creds)
static __inline__ int scm_check_creds(struct ucred *creds)
{
{
        if ((creds->pid == current->pid || capable(CAP_SYS_ADMIN)) &&
        if ((creds->pid == current->pid || capable(CAP_SYS_ADMIN)) &&
            ((creds->uid == current->uid || creds->uid == current->euid ||
            ((creds->uid == current->uid || creds->uid == current->euid ||
              creds->uid == current->suid) || capable(CAP_SETUID)) &&
              creds->uid == current->suid) || capable(CAP_SETUID)) &&
            ((creds->gid == current->gid || creds->gid == current->egid ||
            ((creds->gid == current->gid || creds->gid == current->egid ||
              creds->gid == current->sgid) || capable(CAP_SETGID))) {
              creds->gid == current->sgid) || capable(CAP_SETGID))) {
               return 0;
               return 0;
        }
        }
        return -EPERM;
        return -EPERM;
}
}
 
 
static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
{
{
        int *fdp = (int*)CMSG_DATA(cmsg);
        int *fdp = (int*)CMSG_DATA(cmsg);
        struct scm_fp_list *fpl = *fplp;
        struct scm_fp_list *fpl = *fplp;
        struct file **fpp;
        struct file **fpp;
        int i, num;
        int i, num;
 
 
        num = (cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)))/sizeof(int);
        num = (cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)))/sizeof(int);
 
 
        if (num <= 0)
        if (num <= 0)
                return 0;
                return 0;
 
 
        if (num > SCM_MAX_FD)
        if (num > SCM_MAX_FD)
                return -EINVAL;
                return -EINVAL;
 
 
        if (!fpl)
        if (!fpl)
        {
        {
                fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL);
                fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL);
                if (!fpl)
                if (!fpl)
                        return -ENOMEM;
                        return -ENOMEM;
                *fplp = fpl;
                *fplp = fpl;
                fpl->count = 0;
                fpl->count = 0;
        }
        }
        fpp = &fpl->fp[fpl->count];
        fpp = &fpl->fp[fpl->count];
 
 
        if (fpl->count + num > SCM_MAX_FD)
        if (fpl->count + num > SCM_MAX_FD)
                return -EINVAL;
                return -EINVAL;
 
 
        /*
        /*
         *      Verify the descriptors and increment the usage count.
         *      Verify the descriptors and increment the usage count.
         */
         */
 
 
        for (i=0; i< num; i++)
        for (i=0; i< num; i++)
        {
        {
                int fd = fdp[i];
                int fd = fdp[i];
                struct file *file;
                struct file *file;
 
 
                if (fd < 0 || !(file = fget(fd)))
                if (fd < 0 || !(file = fget(fd)))
                        return -EBADF;
                        return -EBADF;
                *fpp++ = file;
                *fpp++ = file;
                fpl->count++;
                fpl->count++;
        }
        }
        return num;
        return num;
}
}
 
 
void __scm_destroy(struct scm_cookie *scm)
void __scm_destroy(struct scm_cookie *scm)
{
{
        struct scm_fp_list *fpl = scm->fp;
        struct scm_fp_list *fpl = scm->fp;
        int i;
        int i;
 
 
        if (fpl) {
        if (fpl) {
                scm->fp = NULL;
                scm->fp = NULL;
                for (i=fpl->count-1; i>=0; i--)
                for (i=fpl->count-1; i>=0; i--)
                        fput(fpl->fp[i]);
                        fput(fpl->fp[i]);
                kfree(fpl);
                kfree(fpl);
        }
        }
}
}
 
 
int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
{
{
        struct cmsghdr *cmsg;
        struct cmsghdr *cmsg;
        int err;
        int err;
 
 
        for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
        for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
        {
        {
                err = -EINVAL;
                err = -EINVAL;
 
 
                /* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
                /* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
                /* The first check was omitted in <= 2.2.5. The reasoning was
                /* The first check was omitted in <= 2.2.5. The reasoning was
                   that parser checks cmsg_len in any case, so that
                   that parser checks cmsg_len in any case, so that
                   additional check would be work duplication.
                   additional check would be work duplication.
                   But if cmsg_level is not SOL_SOCKET, we do not check
                   But if cmsg_level is not SOL_SOCKET, we do not check
                   for too short ancillary data object at all! Oops.
                   for too short ancillary data object at all! Oops.
                   OK, let's add it...
                   OK, let's add it...
                 */
                 */
                if (cmsg->cmsg_len < sizeof(struct cmsghdr) ||
                if (cmsg->cmsg_len < sizeof(struct cmsghdr) ||
                    (unsigned long)(((char*)cmsg - (char*)msg->msg_control)
                    (unsigned long)(((char*)cmsg - (char*)msg->msg_control)
                                    + cmsg->cmsg_len) > msg->msg_controllen)
                                    + cmsg->cmsg_len) > msg->msg_controllen)
                        goto error;
                        goto error;
 
 
                if (cmsg->cmsg_level != SOL_SOCKET)
                if (cmsg->cmsg_level != SOL_SOCKET)
                        continue;
                        continue;
 
 
                switch (cmsg->cmsg_type)
                switch (cmsg->cmsg_type)
                {
                {
                case SCM_RIGHTS:
                case SCM_RIGHTS:
                        err=scm_fp_copy(cmsg, &p->fp);
                        err=scm_fp_copy(cmsg, &p->fp);
                        if (err<0)
                        if (err<0)
                                goto error;
                                goto error;
                        break;
                        break;
                case SCM_CREDENTIALS:
                case SCM_CREDENTIALS:
                        if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
                        if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
                                goto error;
                                goto error;
                        memcpy(&p->creds, CMSG_DATA(cmsg), sizeof(struct ucred));
                        memcpy(&p->creds, CMSG_DATA(cmsg), sizeof(struct ucred));
                        err = scm_check_creds(&p->creds);
                        err = scm_check_creds(&p->creds);
                        if (err)
                        if (err)
                                goto error;
                                goto error;
                        break;
                        break;
                default:
                default:
                        goto error;
                        goto error;
                }
                }
        }
        }
 
 
        if (p->fp && !p->fp->count)
        if (p->fp && !p->fp->count)
        {
        {
                kfree(p->fp);
                kfree(p->fp);
                p->fp = NULL;
                p->fp = NULL;
        }
        }
        return 0;
        return 0;
 
 
error:
error:
        scm_destroy(p);
        scm_destroy(p);
        return err;
        return err;
}
}
 
 
int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
{
{
        struct cmsghdr *cm = (struct cmsghdr*)msg->msg_control;
        struct cmsghdr *cm = (struct cmsghdr*)msg->msg_control;
        struct cmsghdr cmhdr;
        struct cmsghdr cmhdr;
        int cmlen = CMSG_LEN(len);
        int cmlen = CMSG_LEN(len);
        int err;
        int err;
 
 
        if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
        if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
                msg->msg_flags |= MSG_CTRUNC;
                msg->msg_flags |= MSG_CTRUNC;
                return 0; /* XXX: return error? check spec. */
                return 0; /* XXX: return error? check spec. */
        }
        }
        if (msg->msg_controllen < cmlen) {
        if (msg->msg_controllen < cmlen) {
                msg->msg_flags |= MSG_CTRUNC;
                msg->msg_flags |= MSG_CTRUNC;
                cmlen = msg->msg_controllen;
                cmlen = msg->msg_controllen;
        }
        }
        cmhdr.cmsg_level = level;
        cmhdr.cmsg_level = level;
        cmhdr.cmsg_type = type;
        cmhdr.cmsg_type = type;
        cmhdr.cmsg_len = cmlen;
        cmhdr.cmsg_len = cmlen;
 
 
        err = -EFAULT;
        err = -EFAULT;
        if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
        if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
                goto out;
                goto out;
        if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
        if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
                goto out;
                goto out;
        cmlen = CMSG_SPACE(len);
        cmlen = CMSG_SPACE(len);
        msg->msg_control += cmlen;
        msg->msg_control += cmlen;
        msg->msg_controllen -= cmlen;
        msg->msg_controllen -= cmlen;
        err = 0;
        err = 0;
out:
out:
        return err;
        return err;
}
}
 
 
void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
{
{
        struct cmsghdr *cm = (struct cmsghdr*)msg->msg_control;
        struct cmsghdr *cm = (struct cmsghdr*)msg->msg_control;
 
 
        int fdmax = 0;
        int fdmax = 0;
        int fdnum = scm->fp->count;
        int fdnum = scm->fp->count;
        struct file **fp = scm->fp->fp;
        struct file **fp = scm->fp->fp;
        int *cmfptr;
        int *cmfptr;
        int err = 0, i;
        int err = 0, i;
 
 
        if (msg->msg_controllen > sizeof(struct cmsghdr))
        if (msg->msg_controllen > sizeof(struct cmsghdr))
                fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr))
                fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr))
                         / sizeof(int));
                         / sizeof(int));
 
 
        if (fdnum < fdmax)
        if (fdnum < fdmax)
                fdmax = fdnum;
                fdmax = fdnum;
 
 
        for (i=0, cmfptr=(int*)CMSG_DATA(cm); i<fdmax; i++, cmfptr++)
        for (i=0, cmfptr=(int*)CMSG_DATA(cm); i<fdmax; i++, cmfptr++)
        {
        {
                int new_fd;
                int new_fd;
                err = get_unused_fd();
                err = get_unused_fd();
                if (err < 0)
                if (err < 0)
                        break;
                        break;
                new_fd = err;
                new_fd = err;
                err = put_user(new_fd, cmfptr);
                err = put_user(new_fd, cmfptr);
                if (err) {
                if (err) {
                        put_unused_fd(new_fd);
                        put_unused_fd(new_fd);
                        break;
                        break;
                }
                }
                /* Bump the usage count and install the file. */
                /* Bump the usage count and install the file. */
                get_file(fp[i]);
                get_file(fp[i]);
                fd_install(new_fd, fp[i]);
                fd_install(new_fd, fp[i]);
        }
        }
 
 
        if (i > 0)
        if (i > 0)
        {
        {
                int cmlen = CMSG_LEN(i*sizeof(int));
                int cmlen = CMSG_LEN(i*sizeof(int));
                if (!err)
                if (!err)
                        err = put_user(SOL_SOCKET, &cm->cmsg_level);
                        err = put_user(SOL_SOCKET, &cm->cmsg_level);
                if (!err)
                if (!err)
                        err = put_user(SCM_RIGHTS, &cm->cmsg_type);
                        err = put_user(SCM_RIGHTS, &cm->cmsg_type);
                if (!err)
                if (!err)
                        err = put_user(cmlen, &cm->cmsg_len);
                        err = put_user(cmlen, &cm->cmsg_len);
                if (!err) {
                if (!err) {
                        cmlen = CMSG_SPACE(i*sizeof(int));
                        cmlen = CMSG_SPACE(i*sizeof(int));
                        msg->msg_control += cmlen;
                        msg->msg_control += cmlen;
                        msg->msg_controllen -= cmlen;
                        msg->msg_controllen -= cmlen;
                }
                }
        }
        }
        if (i < fdnum || (fdnum && fdmax <= 0))
        if (i < fdnum || (fdnum && fdmax <= 0))
                msg->msg_flags |= MSG_CTRUNC;
                msg->msg_flags |= MSG_CTRUNC;
 
 
        /*
        /*
         * All of the files that fit in the message have had their
         * All of the files that fit in the message have had their
         * usage counts incremented, so we just free the list.
         * usage counts incremented, so we just free the list.
         */
         */
        __scm_destroy(scm);
        __scm_destroy(scm);
}
}
 
 
struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
{
{
        struct scm_fp_list *new_fpl;
        struct scm_fp_list *new_fpl;
        int i;
        int i;
 
 
        if (!fpl)
        if (!fpl)
                return NULL;
                return NULL;
 
 
        new_fpl = kmalloc(sizeof(*fpl), GFP_KERNEL);
        new_fpl = kmalloc(sizeof(*fpl), GFP_KERNEL);
        if (new_fpl) {
        if (new_fpl) {
                for (i=fpl->count-1; i>=0; i--)
                for (i=fpl->count-1; i>=0; i--)
                        get_file(fpl->fp[i]);
                        get_file(fpl->fp[i]);
                memcpy(new_fpl, fpl, sizeof(*fpl));
                memcpy(new_fpl, fpl, sizeof(*fpl));
        }
        }
        return new_fpl;
        return new_fpl;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.