All of lore.kernel.org
 help / color / mirror / Atom feed
* How is pivot_root intended to be used?
@ 2014-09-01 21:19 Steven Stewart-Gallus
  2014-09-02 19:53 ` Andy Lutomirski
  0 siblings, 1 reply; 3+ messages in thread
From: Steven Stewart-Gallus @ 2014-09-01 21:19 UTC (permalink / raw)
  To: linux-kernel

Hello,

I am not confused about how I can currently use pivot_root for
containers on my kernel (version 3.13). Currently a sequence like:

    if (-1 == syscall(__NR_pivot_root, ".", ".")) {
        perror("pivot_root");
        return EXIT_FAILURE;
    }

    if (-1 == umount2(".", MNT_DETACH)) {
        perror("umount");
        return EXIT_FAILURE;
    }

    /* pivot_root() may or may not affect its current working
     * directory.  It is therefore recommended to call chdir("/")
     * immediately after pivot_root().
     *
     * - http://man7.org/linux/man-pages/man2/pivot_root.2.html
     */
    if (-1 == chdir("/")) {
        perror("chdir");
        return EXIT_FAILURE;
    }

works. However, I am completely and totally confused about how the
pivot_root system call is intended to be used here and have absolutely
no idea if this will work in the future. This concerns me because I
don't want my program to potentially develop silent security bugs on
future versions of the Linux kernel.

Some information about the context. I am sandboxing a program just
after it has done a fork, unshared the mount namespace, setup a
sandbox directory and changed into it. If you just want to look at the
code, the actual code is avaliable at
https://gitorious.org/linted/linted/source/b25685ba4762bfb794c8f36ae74276d32d2b0ca8:src/spawn/spawn.c.

Thank you,
Steven Stewart-Gallus

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How is pivot_root intended to be used?
  2014-09-01 21:19 How is pivot_root intended to be used? Steven Stewart-Gallus
@ 2014-09-02 19:53 ` Andy Lutomirski
  2014-09-04  7:35   ` Steven Stewart-Gallus
  0 siblings, 1 reply; 3+ messages in thread
From: Andy Lutomirski @ 2014-09-02 19:53 UTC (permalink / raw)
  To: Steven Stewart-Gallus, linux-kernel

On 09/01/2014 02:19 PM, Steven Stewart-Gallus wrote:
> Hello,
>
> I am not confused about how I can currently use pivot_root for
> containers on my kernel (version 3.13). Currently a sequence like:
>
>      if (-1 == syscall(__NR_pivot_root, ".", ".")) {
>          perror("pivot_root");
>          return EXIT_FAILURE;
>      }
>
>      if (-1 == umount2(".", MNT_DETACH)) {
>          perror("umount");
>          return EXIT_FAILURE;

Given the comment, you don't know what '.' refers to in the umount2 call 
above.  In fact, I think you're actually detaching the wrong thing, 
leaving possible security issues.

See:

https://github.com/sandstorm-io/sandstorm/blob/master/src/sandstorm/supervisor-main.c%2B%2B#L922

for a program that does this more carefully.

--Andy

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How is pivot_root intended to be used?
  2014-09-02 19:53 ` Andy Lutomirski
@ 2014-09-04  7:35   ` Steven Stewart-Gallus
  0 siblings, 0 replies; 3+ messages in thread
From: Steven Stewart-Gallus @ 2014-09-04  7:35 UTC (permalink / raw)
  To: Andy Lutomirski; +Cc: linux-kernel

Thank you, I think I will change the code to be like:

    int old_root = open("/", O_DIRECTORY);
    if (-1 == old_root) {
        perror("open");
        return EXIT_FAILURE;
    }

    if (-1 == syscall(__NR_pivot_root, ".", ".")) {
        perror("pivot_root");
        return EXIT_FAILURE;
    }

    if (-1 == fchdir(old_root)) {
        perror("fchdir");
        return EXIT_FAILURE;
    }

    if (-1 == umount2(".", MNT_DETACH)) {
        perror("umount");
        return EXIT_FAILURE;
    }

    if (-1 == close(old_root)) {
        perror("close");
        return EXIT_FAILURE;
    }

    if (-1 == chdir("/")) {
        perror("chdir");
        return EXIT_FAILURE;
    }

This seems more understandable to me and less likely to have bugs later on.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-09-04  7:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-01 21:19 How is pivot_root intended to be used? Steven Stewart-Gallus
2014-09-02 19:53 ` Andy Lutomirski
2014-09-04  7:35   ` Steven Stewart-Gallus

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.