linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ram <linuxram@us.ibm.com>
To: Miklos Szeredi <miklos@szeredi.hu>
Cc: jamie@shareable.org, ericvh@gmail.com, 7eggert@gmx.de,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	smfrench@austin.rr.com, hch@infradead.org
Subject: Re: [RCF] [PATCH] unprivileged mount/umount
Date: Thu, 12 May 2005 18:10:20 -0700	[thread overview]
Message-ID: <1115946620.6248.299.camel@localhost> (raw)
In-Reply-To: <E1DWIms-0005nC-00@dorka.pomaz.szeredi.hu>

On Thu, 2005-05-12 at 11:51, Miklos Szeredi wrote:
> > > I'm not sure passing directory file descriptors is the right semantic
> > > we want - but at least it provides a point of explicit control (in
> > > much the same way as a bind).  Are you sure the clone + open("/") +
> > > pass-to-parent scenario you allows the parent to traverse the child's
> > > private name space through that fd?
> > 
> > Pretty sure.
> 
> Yup.  Attached a little program that can be used to try this out.  It
> creates a new namespace in the child, does a bind mount (so the
> namespaces can be differentiated), then sends the file descriptor of
> "/" to the parent.  The parent does fchdir(fd), then starts a shell.


> So the result is that CWD is under the child namespace, while root is
> under the initial namespace.
> 

r u sure, this program works? Sorry if I am saying something dumb here.
Correct me.  When a file descriptor is sent from one process to other,
arn't they referring to different files in each of the processes.
fd=5 may be pointing to file 'xyz' in parent process, 
where as fd=5 will be pointing to 'abc' in the child process.  

This program did not work for me, and I was wondering if adding
CLONE_FILES in clone() would help. Because that would make sure
 that both
the processes share the same file descriptor. It did not work too.

What am I understanding wrong?

In any case my opinion is if this program works than the hole should
be closed instead of exploting it to access different namespace. I 
know Jamie is going to pounce at me. ;)

RP



> I also tried bind mounting from the child's namespace to the parent's,
> and that works too.  But the new mount's mnt_namespace is copied from
> the old, which makes the mount un-removable.  This is most likely not
> intentional, IOW a bug.
> 
> Miklos
> 
> === newns.c =========================================================
> #define _GNU_SOURCE
> 
> #include <stdio.h>
> #include <unistd.h>
> #include <stdlib.h>
> #include <signal.h>
> #include <sched.h>
> #include <errno.h>
> #include <fcntl.h>
> #include <unistd.h>
> #include <sys/un.h>
> #include <sys/socket.h>
> 
> static int socks[2];
> 
> static int send_fd(int sock_fd, int fd)
> {
>     int retval;
>     struct msghdr msg;
>     struct cmsghdr *p_cmsg;
>     struct iovec vec;
>     char cmsgbuf[CMSG_SPACE(sizeof(fd))];
>     int *p_fds;
>     char sendchar = 0;
> 
>     msg.msg_control = cmsgbuf;
>     msg.msg_controllen = sizeof(cmsgbuf);
>     p_cmsg = CMSG_FIRSTHDR(&msg);
>     p_cmsg->cmsg_level = SOL_SOCKET;
>     p_cmsg->cmsg_type = SCM_RIGHTS;
>     p_cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
>     p_fds = (int *) CMSG_DATA(p_cmsg);
>     *p_fds = fd;
>     msg.msg_controllen = p_cmsg->cmsg_len;
>     msg.msg_name = NULL;
>     msg.msg_namelen = 0;
>     msg.msg_iov = &vec;
>     msg.msg_iovlen = 1;
>     msg.msg_flags = 0;
>     /* "To pass file descriptors or credentials you need to send/read at
>      * least one byte" (man 7 unix) */
>     vec.iov_base = &sendchar;
>     vec.iov_len = sizeof(sendchar);
>     while ((retval = sendmsg(sock_fd, &msg, 0)) == -1 && errno == EINTR);
>     if (retval != 1) {
>         perror("sending file descriptor");
>         return -1;
>     }
>     return 0;
> }
> 
> static int receive_fd(int fd)
> {
>     struct msghdr msg;
>     struct iovec iov;
>     char buf[1];
>     int rv;
>     int connfd = -1;
>     char ccmsg[CMSG_SPACE(sizeof(connfd))];
>     struct cmsghdr *cmsg;
> 
>     iov.iov_base = buf;
>     iov.iov_len = 1;
> 
>     msg.msg_name = 0;
>     msg.msg_namelen = 0;
>     msg.msg_iov = &iov;
>     msg.msg_iovlen = 1;
>     /* old BSD implementations should use msg_accrights instead of
>      * msg_control; the interface is different. */
>     msg.msg_control = ccmsg;
>     msg.msg_controllen = sizeof(ccmsg);
> 
>     while(((rv = recvmsg(fd, &msg, 0)) == -1) && errno == EINTR);
>     if (rv == -1) {
>         perror("recvmsg");
>         return -1;
>     }
>     if(!rv) {
>         /* EOF */
>         return -1;
>     }
> 
>     cmsg = CMSG_FIRSTHDR(&msg);
>     if (!cmsg->cmsg_type == SCM_RIGHTS) {
>         fprintf(stderr, "got control message of unknown type %d\n",
>                 cmsg->cmsg_type);
>         return -1;
>     }
>     return *(int*)CMSG_DATA(cmsg);
> }
> 
> int childfn(void *p)
> {
>     int fd;
> 
>     (void) p;
>     mkdir("/tmp/clonetest", 755);
>     mkdir("/tmp/clonetest/dir1", 755);
>     mkdir("/tmp/clonetest/dir1/subdir1", 755);
>     mkdir("/tmp/clonetest/mnt", 755);
>     system("mount --bind /tmp/clonetest/dir1 /tmp/clonetest/mnt");
>     fd = open("/", O_RDONLY | O_DIRECTORY);
>     send_fd(socks[0], fd);
>     sleep(1000);
>     return 1;
> }
> 
> int main()
> {
>     char buf[10000];
>     pid_t pid;
>     int res;
>     int childfd;
> 
>     res = socketpair(AF_UNIX, SOCK_STREAM, 0, socks);
>     if (res == -1) {
>         perror("socketpair");
>         return 1;
>     }
> 
>     pid = clone(childfn, buf+5000, CLONE_NEWNS | SIGCHLD, NULL);
>     if ((int) pid == -1) {
>         perror("clone");
>         exit(1);
>     }
> 
>     childfd = receive_fd(socks[1]);
>     res = fchdir(childfd);
>     if (res == -1) {
>         perror("fchdir");
>         return 1;
>     }
>     execl("/bin/bash", "/bin/bash", NULL);
>     
>     return 0;
> }

  parent reply	other threads:[~2005-05-13  1:10 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <406SQ-5P9-5@gated-at.bofh.it>
     [not found] ` <40rNB-6p8-3@gated-at.bofh.it>
     [not found]   ` <40t37-7ol-5@gated-at.bofh.it>
     [not found]     ` <42VeB-8hG-3@gated-at.bofh.it>
     [not found]       ` <42WNo-1eJ-17@gated-at.bofh.it>
2005-05-11 16:41         ` [RCF] [PATCH] unprivileged mount/umount Bodo Eggert <harvested.in.lkml@posting.7eggert.dyndns.org>
2005-05-11 17:07           ` Jamie Lokier
2005-05-11 18:49             ` Miklos Szeredi
2005-05-11 19:05               ` serue
2005-05-11 19:46                 ` Bodo Eggert
2005-05-11 20:40                   ` Miklos Szeredi
2005-05-11 21:11                 ` Jamie Lokier
2005-05-12  3:05                   ` serue
2005-05-11 19:35               ` Ram
2005-05-11 20:31                 ` Miklos Szeredi
2005-05-11 21:28                 ` Jamie Lokier
2005-05-11 22:42                   ` Ram
2005-05-11 22:58                     ` Eric Van Hensbergen
2005-05-12  1:02                       ` Jamie Lokier
2005-05-12  2:18                         ` Eric Van Hensbergen
2005-05-12  6:45                           ` Jamie Lokier
2005-05-12 13:23                             ` Eric Van Hensbergen
2005-05-12 13:47                               ` serue
2005-05-12 15:16                               ` Jamie Lokier
2005-05-12 12:51                                 ` serue
2005-05-12 18:51                                 ` Miklos Szeredi
2005-05-12 19:56                                   ` Jamie Lokier
2005-05-13  8:55                                     ` Miklos Szeredi
2005-05-13  1:10                                   ` Ram [this message]
2005-05-13  6:06                                     ` Miklos Szeredi
2005-05-13  7:25                                     ` Ram
2005-05-13  8:59                                       ` Ram
2005-05-13  9:10                                         ` Miklos Szeredi
2005-05-13 16:53                                           ` Ram
2005-05-13 17:14                                             ` Miklos Szeredi
2005-05-13 18:44                                             ` Alan Cox
2005-05-13 20:56                                     ` Bryan Henderson
2005-05-12  0:59                     ` Jamie Lokier
2005-05-13  6:41                       ` Ram
2005-05-11 21:09               ` Jamie Lokier
2005-05-11 21:20                 ` Miklos Szeredi
2005-05-11 21:32                   ` Jamie Lokier
2005-05-11 19:32             ` Bodo Eggert
2005-05-11 21:23               ` Jamie Lokier
2005-05-11 21:34                 ` Miklos Szeredi
2005-05-11 21:36                   ` Jamie Lokier
2005-05-12  3:08                     ` serue
2005-05-03 14:31 Miklos Szeredi
2005-05-04 13:08 ` Eric Van Hensbergen
2005-05-04 14:21   ` Miklos Szeredi
2005-05-04 14:51     ` Eric Van Hensbergen
2005-05-04 15:21       ` Miklos Szeredi
2005-05-11  8:51     ` Christoph Hellwig
2005-05-11 10:31       ` Miklos Szeredi
2005-05-12 21:08         ` Bryan Henderson
2005-05-13  5:47           ` Miklos Szeredi
2005-05-13  7:19             ` Jan Hudec
2005-05-13  8:33               ` Miklos Szeredi
2005-05-13 23:09                 ` Bryan Henderson
2005-05-14  6:58                   ` Miklos Szeredi
2005-05-16 18:35                     ` Bryan Henderson
2005-05-14 11:49                   ` Jamie Lokier
2005-05-04 13:47 ` Martin Waitz
2005-05-04 14:34   ` Miklos Szeredi
2005-05-11  8:53   ` Christoph Hellwig
2005-05-11  8:48 ` Christoph Hellwig
2005-05-11 10:20   ` Miklos Szeredi
2005-05-16  9:34     ` Christoph Hellwig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1115946620.6248.299.camel@localhost \
    --to=linuxram@us.ibm.com \
    --cc=7eggert@gmx.de \
    --cc=ericvh@gmail.com \
    --cc=hch@infradead.org \
    --cc=jamie@shareable.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=miklos@szeredi.hu \
    --cc=smfrench@austin.rr.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).