summaryrefslogtreecommitdiff
path: root/nsmount.c
blob: 9d1533a98ce86a835e6a55d45c2c98010c652393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
 *  nsmount - mount a block device into a mount/pid namespace
 *  Copyright (C) 2019  Anton Khirnov <anton@khirnov.net>
 *
 *  nsmount 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  nsmount is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with nsmount.  If not, see <http://www.gnu.org/licenses/>.
 */

#define _XOPEN_SOURCE 700
#define _GNU_SOURCE

#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/mount.h>
#include <sys/types.h>
#include <sys/wait.h>

enum {
    OP_MOUNT,
    OP_UMOUNT,
};

static void print_usage(int argc, const char * const *argv)
{
    fprintf(stderr,
            "%s: mount/unmount a block device in a mount/PID namespace\n\n"
            "Usage:\n"
            "   %s m <PID> <mountpoint> <blkdev_path> <fstype>\n"
            "   %s u <PID> <mountpoint>\n\n"
            " <PID>: PID (in the namespace in which this program is executed)"
                " of the process whose namespaces are to be entered into\n"
            " <mountpoint>: path (in the destination mount namespace) to be mounted"
                " or unmounted\n"
            " <blkdev_path>: path (in the namespace in which this program is executed)"
                " to the block device that shall be mounted\n"
            " <fstype>: type of the filesystem to be mounted\n",
            argv[0], argv[0], argv[0]);
}

int main(int argc, const char * const *argv)
{
    char pathbuf[128];
    int blockdev_fd = -1, pidns_fd = -1, mountns_fd = -1;
    const char *blockdev, *mountpoint, *fstype;
    pid_t tgt_pid, child_pid;
    int op;
    int ret;

    /* parse the commandline */
    if (argc < 2) {
        print_usage(argc, argv);
        return 1;
    }

    if (argv[1][0] == 'm') {
        op = OP_MOUNT;
        if (argc < 6) {
            print_usage(argc, argv);
            return 1;
        }
    } else if (argv[1][0] == 'u') {
        op = OP_UMOUNT;
        if (argc < 4) {
            print_usage(argc, argv);
            return 1;
        }
    } else {
        fprintf(stderr, "Invalid operation: %s\n",
                argv[1]);
        print_usage(argc, argv);
        return  1;
    }

    tgt_pid  = strtol(argv[2], NULL, 0);
    mountpoint = argv[3];
    if (op == OP_MOUNT) {
        blockdev = argv[4];
        fstype   = argv[5];
    }

    /* open the files */
    if (op == OP_MOUNT) {
        blockdev_fd = open(blockdev, O_RDONLY);
        if (blockdev_fd == -1) {
            fprintf(stderr, "Error opening %s: %s\n",
                    blockdev, strerror(errno));
            return 2;
        }
    }

    ret = snprintf(pathbuf, sizeof(pathbuf), "/proc/%d/ns/pid",
                   tgt_pid);
    if (ret < 0 || ret >= sizeof(pathbuf)) {
        fprintf(stderr, "Error constructing the PID namespace path\n");
        ret = 2;
        goto finish;
    }

    pidns_fd = open(pathbuf, O_RDONLY | O_CLOEXEC);
    if (pidns_fd == -1) {
        fprintf(stderr, "Error opening %s: %s\n",
                pathbuf, strerror(errno));
        ret = 2;
        goto finish;
    }

    ret = snprintf(pathbuf, sizeof(pathbuf), "/proc/%d/ns/mnt",
                   tgt_pid);
    if (ret < 0 || ret >= sizeof(pathbuf)) {
        fprintf(stderr, "Error constructing the mount namespace path\n");
        ret = 2;
        goto finish;
    }

    mountns_fd = open(pathbuf, O_RDONLY | O_CLOEXEC);
    if (mountns_fd == -1) {
        fprintf(stderr, "Error opening %s: %s\n",
                pathbuf, strerror(errno));
        ret = 2;
        goto finish;
    }

    /* enter the namespaces */
    ret = setns(pidns_fd, CLONE_NEWPID);
    if (ret == -1) {
        fprintf(stderr, "Error entering the PID namespace: %s\n",
                strerror(errno));
        ret = 3;
        goto finish;
    }

    ret = setns(mountns_fd, CLONE_NEWNS);
    if (ret == -1) {
        fprintf(stderr, "Error entering the mount namespace: %s\n",
                strerror(errno));
        ret = 3;
        goto finish;
    }

    /* fork to actually enter the PID namespace */
    child_pid = fork();
    if (child_pid == -1) {
        fprintf(stderr, "fork() failed: %s\n",
                strerror(errno));
        ret = 4;
        goto finish;
    }

    if (child_pid) {
        /* we are the parent */
        ret = wait(NULL);
        if (ret == -1) {
            fprintf(stderr, "Error waiting for the child: %s\n",
                    strerror(errno));
            ret = 4;
            goto finish;
        }
    } else {
        /* we are the child */
        if (op == OP_MOUNT) {
            /* we use /proc/self/fd to mount the device
             * Since the container controls its own filesystem hierarchy, it
             * could trick us into mounting an arbitrary node located in the
             * filesystem. This is not considered a major security problem,
             * since
             *  - the container should not have access to mknod() or nodes that
             *    it is not meant to read
             *  - we mount the filesystem read-only, with nosuid flag
             *  - since the container will typically live in its own user
             *    namespace, it will not have the right permissions to access a
             *    filesystem that is not intended for it
             *
             *  Ideally, there would be something like a mountfd() syscall that
             *  would allow mounting an fd.
             */
            ret = snprintf(pathbuf, sizeof(pathbuf),
                           "/proc/self/fd/%d", blockdev_fd);
            if (ret < 0 || ret >= sizeof(pathbuf)) {
                fprintf(stderr, "Error constructing the mount path\n");
                ret = 4;
                goto finish;
            }

            ret = mount(pathbuf, mountpoint, fstype, MS_RDONLY | MS_NOSUID, NULL);
            if (ret == -1) {
                fprintf(stderr, "mount(%s, %s) failed: %s\n",
                        pathbuf, mountpoint, strerror(errno));
                ret = 5;
                goto finish;
            }
        } else if (op == OP_UMOUNT) {
            /**
             * As above, a malicious container can trick us into unmounting a
             * filesystem in its tree. This should not cause any issues other
             * than disrupting the container (which a compromised container can
             * already do without our help).
             */
            ret = umount(mountpoint);
            if (ret == -1) {
                fprintf(stderr, "umount() failed: %s\n", strerror(errno));
                ret = 5;
                goto finish;
            }
        }
    }

    ret = 0;
finish:
    if (blockdev_fd >= 0)
        close(blockdev_fd);
    if (pidns_fd >= 0)
        close(pidns_fd);
    if (mountns_fd >= 0)
        close(mountns_fd);

    return ret;
}