-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathuserbindmount.3.html
233 lines (183 loc) · 6.92 KB
/
userbindmount.3.html
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
Content-type: text/html; charset=UTF-8
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of LIBUSERBINDMOUNT</TITLE>
</HEAD><BODY>
<H1>LIBUSERBINDMOUNT</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2017-08-22<BR><A HREF="#index">Index</A>
<A HREF="/#/man/index">Return to Main Contents</A><HR>
<A NAME="lbAB"> </A>
<H2>NAME</H2>
userbindmount, userbindmount_unshare, userbindmount_set_cap_sysadm,
userbindmount_string, userbindmount_data, userbindmount_fd - bind-mount in user-namespaces
<A NAME="lbAC"> </A>
<H2>SYNOPSIS</H2>
<B>#include <<A HREF="file:///usr/include/userbindmount.h">userbindmount.h</A>></B>
<BR>
<B>int userbindmount(const char *</B><I>source</I><B>, const char *</B><I>target</I><B>);</B>
<BR>
<B>int userbindmount_unshare(void);</B>
<BR>
<B>int userbindmount_set_cap_sysadm(void);</B>
<BR>
<B>int userbindmount_string(const char *</B><I>string</I><B>, const char *</B><I>target</I><B>, mode_t </B><I>mode</I><B>);</B>
<BR>
<B>int userbindmount_data(const void *</B><I>data</I><B>, size_t </B><I>count</I><B>, const char *</B><I>target</I><B>, mode_t </B><I>mode</I><B>);</B>
<BR>
<B>int userbindmount_fd(int </B><I>fd</I><B>, const char *</B><I>target</I><B>, mode_t </B><I>mode</I><B>);</B>
<P>
These functions are provided by libuserbindmount. Link with <I>-luserbindmount</I>.
<A NAME="lbAD"> </A>
<H2>DESCRIPTION</H2>
Libuserbindmount is a library providing support for bind mount in user userspaces.
<P>
<B>userbindmount</B> bind mounts <I>source</I> on <I>destination</I>.
If it is not permitted in the current namespace it creates (unshare) a new user-namespace.
<P>
<B>userbindmount_unshare</B> creates a new user-namespace where bind mount is allowed.
<P>
<B>userbindmount_set_cap_sysadm</B> add the CAP_SYS_ADMIN ambient capability to the current namespace so that the
ability of bind mount files and directories can be exported to new programs (ambient capabilities survive to <A HREF="/#/man/man2/execve.2.html">execve</A>(2)).
<P>
<B>userbindmount_string</B> bind-mounts on <I>destination</I> a temporary file whose (text) contents is provided
by <I>string</I>. The temporary file is automatically deleted when the namespace is closed or
the file/directory unmounted.
<P>
<B>userbindmount_data</B> bind-mounts on <I>destination</I> a temporary file whose (binary) contents is provided
by <I>data</I> and has the size of <I>count</I> bytes. The temporary file is automatically deleted when
the namespace is closed or the file/directory unmounted.
<P>
<B>userbindmount_fd</B> bind-mounts on <I>destination</I> a temporary file whose contents is read
from the by the file descriptor <I>fd</I> (up to the end of file). The temporary file is automatically
deleted when the namespace is closed or the file/directory unmounted.
<P>
<A NAME="lbAE"> </A>
<H2>RETURN VALUE</H2>
<P>
All the functions provided by libuserbindmount return 0 in case of success.
-1 is returned elseways and errno is set appropriately.
<P>
<A NAME="lbAF"> </A>
<H2>NOTES</H2>
Libuserbindmount fails if user namespaces have not been configured in the running kernel and enabled for users.
In Debian the sysctl knob <B>kernel.unprivileged_userns_clone</B> must be set to 1.
<P>
<A NAME="lbAG"> </A>
<H2>EXAMPLES</H2>
The following excerpts of C code shows the use of <B>libuserbindmount</B>:
the inclusion of the header file for this library is required:
<DL COMPACT><DT><DD>
<PRE>
#include <<A HREF="file:///usr/include/usrbindmount.h">usrbindmount.h</A>>
</PRE>
</DL>
<P>
<P>
Bind-mount /tmp/resolv.conf on /etc/resolv.conf:
<DL COMPACT><DT><DD>
<PRE>
userbindmount("/tmp/resolv.conf", "/etc/resolv.conf");
</PRE>
</DL>
<P>
<P>
Bind-mount a string on /etc/resolv.conf:
<DL COMPACT><DT><DD>
<PRE>
userbindmount_string("nameserver 9.9.9.9, "/etc/resolv.conf", 0600);
</PRE>
</DL>
<P>
<P>
Bind-mount a binary data on /proc/self/cmdline:
<DL COMPACT><DT><DD>
<PRE>
static char fakeargv[] = {'c','m','d',0,
'a','r','g','1',0,
'a','r','g','2',0,
0};
userbindmount_data(fakeargv, sizeof(fakeargv), "/proc/self/cmdline", 0600);
</PRE>
</DL>
<P>
Bind-mount the data read from a file descriptor on /etc/resolv.conf:
<DL COMPACT><DT><DD>
<PRE>
userbindmount_fd(STDIN_FILENO, "/etc/resolv.conf", 0600);
</PRE>
</DL>
<P>
<P>
Bind-mount several files or directories:
<DL COMPACT><DT><DD>
<PRE>
userbindmount("/tmp/resolv.conf", "/etc/resolv.conf");
userbindmount("/tmp/passwd", "/etc/passwd");
userbindmount("/tmp/hosts", "/etc/hosts");
</PRE>
</DL>
Only the first userbindmount creates a new namespace if needed.
<P>
<P>
The following program creates a namespace and runs a program in it.
In the new namespace bind-mount is allowed.
<DL COMPACT><DT><DD>
<PRE>
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
#include <<A HREF="file:///usr/include/userbindmount.h">userbindmount.h</A>>
int main(int argc, char *argv[]) {
userbindmount_unshare();
userbindmount_set_cap_sysadm();
execvp(argv[1], argv+1);
}
</PRE>
</DL>
<P>
It can be compiled and tested in the following way:
<DL COMPACT><DT><DD>
<PRE>
$ gcc -o unshare_sysadm unshare_sysadm.c -luserbindmount
$ unshare_sysadm bash
$ cat /etc/resolv.conf
nameserver 127.0.0.1
$ echo "nameserver 9.9.9.9" > /tmp/resolv.conf
$ busybox mount --bind /tmp/resolv.conf /etc/resolv.conf
$ cat /etc/resolv.conf
nameserver 9.9.9.9
$ exit
$
</PRE>
</DL>
please note that in the example the mount command by busybox has been used instead of the standard mount by util-linux. In fact the standard mount command has not been updated to support the capabilities, and forbids the access to the mount system call if the effective user is not root, denying in this way a legal operation.
<P>
<A NAME="lbAH"> </A>
<H2>SEE ALSO</H2>
<B><A HREF="/#/man/man2/mount.2.html">mount</A>(2),</B> <A HREF="/#/man/man8/mount.8.html">mount</A>(8),<B> <A HREF="/#/man/man7/user_namespaces.7.html">user_namespaces</A>(7)</B>
<P>
<A NAME="lbAI"> </A>
<H2>BUGS</H2>
Bug reports should be addressed to <<A HREF="mailto:[email protected]">[email protected]</A>>
<A NAME="lbAJ"> </A>
<H2>AUTHORS</H2>
Renzo Davoli <<A HREF="mailto:[email protected]">[email protected]</A>>
<P>
<HR>
<A NAME="index"> </A><H2>Index</H2>
<DL>
<DT><A HREF="#lbAB">NAME</A><DD>
<DT><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT><A HREF="#lbAF">NOTES</A><DD>
<DT><A HREF="#lbAG">EXAMPLES</A><DD>
<DT><A HREF="#lbAH">SEE ALSO</A><DD>
<DT><A HREF="#lbAI">BUGS</A><DD>
<DT><A HREF="#lbAJ">AUTHORS</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 15:22:07 GMT, November 27, 2023
</BODY>
</HTML>