Debian Codegraph @

runtime/linux/stap_fs.h

raw
1 /* -*- linux-c -*-
2 * Utility functions for handling file systems and mount namespaces.
3 *
4 * Copyright (C) 2023 by OpenResty Inc.
5 * This file is part of systemtap, and is free software. You can
6 * redistribute it and/or modify it under the terms of the GNU General
7 * Public License (GPL); either version 2, or (at your option) any
8 * later version.
9 */
10
11 #ifndef LINUX_STAP_FS_H
12 #define LINUX_STAP_FS_H
13
14 #include <linux/namei.h>
15 #include <linux/fs_struct.h>
16 #include <linux/nsproxy.h>
17 #include <linux/proc_ns.h>
18
19 static int _stp_target_mnt_ns_fd = -1;
20 static int _stp_orig_mnt_ns_fd = -1;
21 static bool _stp_fs_struct_unshared = false;
22
23 static inline bool
24 has_set_mnt_ns(void)
25 {
26 static int cached_res = -1;
27 if (cached_res != -1)
28 return cached_res;
29 cached_res =
30 (kallsyms_proc_ns_file && kallsyms_unshare_nsproxy_namespaces
31 && kallsyms_switch_task_namespaces && kallsyms_free_nsproxy)
32 ;
33
34 return cached_res;
35 }
36
37 static inline int
38 stap_set_mnt_ns(int fd)
39 {
40 #ifdef STAPCONF_NSSET_COMPLETE
41 struct file *file;
42 struct nsset nsset = {};
43 struct ns_common *ns = NULL;
44 int err = 0;
45 struct task_struct *me;
46
47 if (!kallsyms_proc_ns_file || !kallsyms_unshare_nsproxy_namespaces
48 || !kallsyms_switch_task_namespaces || !kallsyms_free_nsproxy)
49 {
50 /* NB do nothing; ignore the error when the kernel is too
51 * old to support this. */
52 return 0;
53 }
54
55 dbug_task(2, "using our own setns() impl instead of the syscall\n");
56
57 me = current;
58
59 file = fget(fd);
60 if (!file)
61 return -EBADF;
62
63 /* unlike the setns() syscall, we don't allow non-proc ns file */
64 if (!(ibt_wrapper(bool,
65 (*(proc_ns_file_fn) kallsyms_proc_ns_file)(file))))
66 err = -EINVAL;
67
68 if (unlikely(err))
69 goto out;
70
71 ns = get_proc_ns(file_inode(file));
72 #if defined(STAPCONF_PR33574_PNO_TYPE)
73 if (ns->ops->type != CLONE_NEWNS) {
74 #else
75 if (ns->ns_type != CLONE_NEWNS) {
76 #endif
77 err = -EINVAL;
78 goto out;
79 }
80
81 /* NB: Alas. we actually need to call create_nsproxy_namesapes() here
82 * but it is an internal symbol declared by 'static'. The
83 * unshare_nsproxy_namespaces() function is the closest symbol we can
84 * use; but it allocates a new mnt ns we don't need. */
85 err = ibt_wrapper(int, (*(unshare_nsproxy_namespaces_fn)
86 kallsyms_unshare_nsproxy_namespaces)(CLONE_NEWNS, &nsset.nsproxy,
87 NULL, NULL));
88 if (unlikely(err)) {
89 goto out;
90 }
91
92 if (unlikely(nsset.nsproxy == NULL)) {
93 err = -ENOMEM;
94 goto out;
95 }
96
97 nsset.cred = current_cred();
98 if (unlikely(!nsset.cred)) {
99 err = -ENOMEM;
100 goto out0;
101 }
102
103 nsset.fs = me->fs;
104 nsset.flags = CLONE_NEWNS;
105
106 err = ns->ops->install(&nsset, ns);
107 if (unlikely(!err)) {
108 /* transfer ownership */
109 void_ibt_wrapper((*(switch_task_namespaces_fn) kallsyms_switch_task_namespaces)(me, nsset.nsproxy));
110 nsset.nsproxy = NULL;
111 }
112 out0:
113 if (nsset.nsproxy)
114 void_ibt_wrapper((*(free_nsproxy_fn) kallsyms_free_nsproxy)(nsset.nsproxy));
115 out:
116 fput(file);
117 return err;
118 #else /* !defined(STAPCONF_NSSET_COMPLETE) */
119 /* NB do nothing; ignore the error when the kernel is too
120 * old to support this. */
121 return 0;
122 #endif
123 }
124
125 /* Returns 0 on success or an error code otherwise. */
126 static inline int
127 stap_switch_to_target_mnt_ns_if_needed(bool *switched_ptr)
128 {
129 if (_stp_target_mnt_ns_fd > 0 && has_set_mnt_ns()) {
130 bool just_unshared = false;
131 int rc;
132
133 if (unlikely(! _stp_fs_struct_unshared)) {
134 rc = unshare_fs_struct();
135 if (unlikely(rc != 0)) {
136 _stp_error("unshare_fs_struct() failed: %d\n",
137 rc);
138 return rc;
139 }
140 _stp_fs_struct_unshared = true;
141 just_unshared = true;
142 }
143
144 dbug_task(2, "switching mount namespace to the target process's for task '%s'\n",
145 current ? current->comm : "<null>");
146
147 rc = stap_set_mnt_ns(_stp_target_mnt_ns_fd);
148
149 if (unlikely(rc == -EINVAL && ! just_unshared)) {
150 /* setns() is per-thread so it's still possible for
151 * a new thread to need a call to unshare_fs_struct() */
152 if (unlikely(! _stp_fs_struct_unshared)) {
153 rc = unshare_fs_struct();
154 if (unlikely(rc != 0)) {
155 _stp_error("unshare_fs_struct() failed: %d\n",
156 rc);
157 return rc;
158 }
159 _stp_fs_struct_unshared = true;
160 }
161
162 rc = stap_set_mnt_ns(_stp_target_mnt_ns_fd);
163 }
164
165 if (unlikely(rc != 0)) {
166 _stp_error("setns(%d) failed for the target mount namespace: %d\n",
167 _stp_target_mnt_ns_fd, rc);
168 return rc;
169 }
170
171 *switched_ptr = true;
172 }
173
174 return 0;
175 }
176
177 static inline int
178 stap_switch_to_orig_mnt_ns_if_needed(void)
179 {
180 if (likely(_stp_orig_mnt_ns_fd > 0 && has_set_mnt_ns())) {
181 int rc;
182
183 dbug_task(2, "switching mount namespace to the original one for task '%s'\n",
184 current ? current->comm : "<null>");
185
186 rc = stap_set_mnt_ns(_stp_orig_mnt_ns_fd);
187 if (unlikely(rc != 0)) {
188 _stp_error("setns(%d) failed for the original mount namespace: %d\n",
189 _stp_target_mnt_ns_fd, rc);
190 return rc;
191 }
192 }
193
194 return 0;
195 }
196
197 static inline char *
198 stap_real_path(const char *pathname, size_t *len_ptr)
199 {
200 int rc;
201 char *ret;
202 char *path_buf = NULL;
203 char *p;
204 struct path path;
205 bool mnt_ns_switched = false;
206
207 might_sleep();
208
209 *len_ptr = 0;
210
211 path_buf = _stp_kmalloc(PATH_MAX);
212 if (unlikely(path_buf == NULL)) {
213 ret = ERR_PTR(-ENOMEM);
214 goto out;
215 }
216
217 rc = stap_switch_to_target_mnt_ns_if_needed(&mnt_ns_switched);
218 if (unlikely(rc != 0)) {
219 ret = ERR_PTR(rc);
220 goto out_with_buf;
221 }
222
223 rc = kern_path(pathname, LOOKUP_FOLLOW, &path);
224
225 if (mnt_ns_switched) {
226 int rc;
227 rc = stap_switch_to_orig_mnt_ns_if_needed();
228 if (unlikely(rc != 0)) {
229 ret = ERR_PTR(rc);
230 goto out_with_buf;
231 }
232 }
233
234 if (unlikely(rc != 0)) {
235 _stp_error("Couldn't resolve target program file path '%s': %d\n",
236 pathname, rc);
237 ret = ERR_PTR(rc);
238 goto out_with_buf;
239 }
240
241 p = d_path(&path, path_buf, PATH_MAX);
242 if (unlikely(p == NULL)) {
243 ret = ERR_PTR(-EINVAL);
244 goto out_with_path;
245 }
246
247 if (unlikely(IS_ERR(p))) {
248 _stp_error("d_path() failed for path '%s': %ld\n", pathname,
249 PTR_ERR(p));
250 ret = p;
251 goto out_with_path;
252 }
253
254 #if 0
255 pr_warn("path buf: '%s' -> '%s'\n", pathname, p);
256 #endif
257
258 if (strcmp(p, pathname) != 0) { /* found a symlink */
259 char *q;
260
261 size_t len = strlen(p);
262 if (unlikely(len == 0)) {
263 ret = ERR_PTR(-EINVAL);
264 goto out_with_path;
265 }
266
267 q = _stp_kmalloc(len + 1);
268 if (unlikely(q == NULL)) {
269 ret = ERR_PTR(-ENOMEM);
270 goto out_with_path;
271 }
272
273 memcpy(q, p, len + 1);
274 *len_ptr = len;
275
276 ret = q;
277 goto out_with_path;
278 }
279
280 ret = NULL;
281
282 out_with_path:
283
284 path_put(&path);
285
286 out_with_buf:
287
288 if (likely(path_buf != NULL))
289 _stp_kfree(path_buf);
290 out:
291
292 return ret;
293 }
294
295 #endif /* LINUX_STAP_FS_H */