xref: /linux/tools/perf/util/namespaces.c (revision ec714e371f22f716a04e6ecb2a24988c92b26911)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Copyright (C) 2017 Hari Bathini, IBM Corporation
5  */
6 
7 #include "namespaces.h"
8 #include "event.h"
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <limits.h>
13 #include <sched.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <asm/bug.h>
19 #include <linux/kernel.h>
20 #include <linux/zalloc.h>
21 
22 static const char *perf_ns__names[] = {
23 	[NET_NS_INDEX]		= "net",
24 	[UTS_NS_INDEX]		= "uts",
25 	[IPC_NS_INDEX]		= "ipc",
26 	[PID_NS_INDEX]		= "pid",
27 	[USER_NS_INDEX]		= "user",
28 	[MNT_NS_INDEX]		= "mnt",
29 	[CGROUP_NS_INDEX]	= "cgroup",
30 };
31 
perf_ns__name(unsigned int id)32 const char *perf_ns__name(unsigned int id)
33 {
34 	if (id >= ARRAY_SIZE(perf_ns__names))
35 		return "UNKNOWN";
36 	return perf_ns__names[id];
37 }
38 
namespaces__new(struct perf_record_namespaces * event)39 struct namespaces *namespaces__new(struct perf_record_namespaces *event)
40 {
41 	struct namespaces *namespaces;
42 	u64 link_info_size = ((event ? event->nr_namespaces : NR_NAMESPACES) *
43 			      sizeof(struct perf_ns_link_info));
44 
45 	namespaces = zalloc(sizeof(struct namespaces) + link_info_size);
46 	if (!namespaces)
47 		return NULL;
48 
49 	namespaces->end_time = -1;
50 
51 	if (event)
52 		memcpy(namespaces->link_info, event->link_info, link_info_size);
53 
54 	return namespaces;
55 }
56 
namespaces__free(struct namespaces * namespaces)57 void namespaces__free(struct namespaces *namespaces)
58 {
59 	free(namespaces);
60 }
61 
nsinfo__get_nspid(pid_t * tgid,pid_t * nstgid,bool * in_pidns,const char * path)62 static int nsinfo__get_nspid(pid_t *tgid, pid_t *nstgid, bool *in_pidns, const char *path)
63 {
64 	FILE *f = NULL;
65 	char *statln = NULL;
66 	size_t linesz = 0;
67 	char *nspid;
68 
69 	f = fopen(path, "r");
70 	if (f == NULL)
71 		return -1;
72 
73 	while (getline(&statln, &linesz, f) != -1) {
74 		/* Use tgid if CONFIG_PID_NS is not defined. */
75 		if (strstr(statln, "Tgid:") != NULL) {
76 			*tgid = (pid_t)strtol(strrchr(statln, '\t'), NULL, 10);
77 			*nstgid = *tgid;
78 		}
79 
80 		if (strstr(statln, "NStgid:") != NULL) {
81 			nspid = strrchr(statln, '\t');
82 			*nstgid = (pid_t)strtol(nspid, NULL, 10);
83 			/*
84 			 * If innermost tgid is not the first, process is in a different
85 			 * PID namespace.
86 			 */
87 			*in_pidns = (statln + sizeof("NStgid:") - 1) != nspid;
88 			break;
89 		}
90 	}
91 
92 	fclose(f);
93 	free(statln);
94 	return 0;
95 }
96 
nsinfo__init(struct nsinfo * nsi)97 int nsinfo__init(struct nsinfo *nsi)
98 {
99 	char oldns[PATH_MAX];
100 	char spath[PATH_MAX];
101 	char *newns = NULL;
102 	struct stat old_stat;
103 	struct stat new_stat;
104 	int rv = -1;
105 
106 	if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
107 		return rv;
108 
109 	if (asprintf(&newns, "/proc/%d/ns/mnt", nsinfo__pid(nsi)) == -1)
110 		return rv;
111 
112 	if (stat(oldns, &old_stat) < 0)
113 		goto out;
114 
115 	if (stat(newns, &new_stat) < 0)
116 		goto out;
117 
118 	/* Check if the mount namespaces differ, if so then indicate that we
119 	 * want to switch as part of looking up dso/map data.
120 	 */
121 	if (old_stat.st_ino != new_stat.st_ino) {
122 		RC_CHK_ACCESS(nsi)->need_setns = true;
123 		RC_CHK_ACCESS(nsi)->mntns_path = newns;
124 		newns = NULL;
125 	}
126 
127 	/* If we're dealing with a process that is in a different PID namespace,
128 	 * attempt to work out the innermost tgid for the process.
129 	 */
130 	if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsinfo__pid(nsi)) >= PATH_MAX)
131 		goto out;
132 
133 	rv = nsinfo__get_nspid(&RC_CHK_ACCESS(nsi)->tgid, &RC_CHK_ACCESS(nsi)->nstgid,
134 			       &RC_CHK_ACCESS(nsi)->in_pidns, spath);
135 
136 out:
137 	free(newns);
138 	return rv;
139 }
140 
nsinfo__alloc(void)141 static struct nsinfo *nsinfo__alloc(void)
142 {
143 	struct nsinfo *res;
144 	RC_STRUCT(nsinfo) *nsi;
145 
146 	nsi = calloc(1, sizeof(*nsi));
147 	if (ADD_RC_CHK(res, nsi))
148 		refcount_set(&nsi->refcnt, 1);
149 
150 	return res;
151 }
152 
nsinfo__new(pid_t pid)153 struct nsinfo *nsinfo__new(pid_t pid)
154 {
155 	struct nsinfo *nsi;
156 
157 	if (pid == 0)
158 		return NULL;
159 
160 	nsi = nsinfo__alloc();
161 	if (!nsi)
162 		return NULL;
163 
164 	RC_CHK_ACCESS(nsi)->pid = pid;
165 	RC_CHK_ACCESS(nsi)->tgid = pid;
166 	RC_CHK_ACCESS(nsi)->nstgid = pid;
167 	nsinfo__clear_need_setns(nsi);
168 	RC_CHK_ACCESS(nsi)->in_pidns = false;
169 	/* Init may fail if the process exits while we're trying to look at its
170 	 * proc information. In that case, save the pid but don't try to enter
171 	 * the namespace.
172 	 */
173 	if (nsinfo__init(nsi) == -1)
174 		nsinfo__clear_need_setns(nsi);
175 
176 	return nsi;
177 }
178 
nsinfo__mntns_path(const struct nsinfo * nsi)179 static const char *nsinfo__mntns_path(const struct nsinfo *nsi)
180 {
181 	return RC_CHK_ACCESS(nsi)->mntns_path;
182 }
183 
nsinfo__copy(const struct nsinfo * nsi)184 struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
185 {
186 	struct nsinfo *nnsi;
187 
188 	if (nsi == NULL)
189 		return NULL;
190 
191 	nnsi = nsinfo__alloc();
192 	if (!nnsi)
193 		return NULL;
194 
195 	RC_CHK_ACCESS(nnsi)->pid = nsinfo__pid(nsi);
196 	RC_CHK_ACCESS(nnsi)->tgid = nsinfo__tgid(nsi);
197 	RC_CHK_ACCESS(nnsi)->nstgid = nsinfo__nstgid(nsi);
198 	RC_CHK_ACCESS(nnsi)->need_setns = nsinfo__need_setns(nsi);
199 	RC_CHK_ACCESS(nnsi)->in_pidns = nsinfo__in_pidns(nsi);
200 	if (nsinfo__mntns_path(nsi)) {
201 		RC_CHK_ACCESS(nnsi)->mntns_path = strdup(nsinfo__mntns_path(nsi));
202 		if (!RC_CHK_ACCESS(nnsi)->mntns_path) {
203 			nsinfo__put(nnsi);
204 			return NULL;
205 		}
206 	}
207 
208 	return nnsi;
209 }
210 
nsinfo__refcnt(struct nsinfo * nsi)211 static refcount_t *nsinfo__refcnt(struct nsinfo *nsi)
212 {
213 	return &RC_CHK_ACCESS(nsi)->refcnt;
214 }
215 
nsinfo__delete(struct nsinfo * nsi)216 static void nsinfo__delete(struct nsinfo *nsi)
217 {
218 	if (nsi) {
219 		WARN_ONCE(refcount_read(nsinfo__refcnt(nsi)) != 0, "nsinfo refcnt unbalanced\n");
220 		zfree(&RC_CHK_ACCESS(nsi)->mntns_path);
221 		RC_CHK_FREE(nsi);
222 	}
223 }
224 
nsinfo__get(struct nsinfo * nsi)225 struct nsinfo *nsinfo__get(struct nsinfo *nsi)
226 {
227 	struct nsinfo *result;
228 
229 	if (RC_CHK_GET(result, nsi))
230 		refcount_inc(nsinfo__refcnt(nsi));
231 
232 	return result;
233 }
234 
nsinfo__put(struct nsinfo * nsi)235 void nsinfo__put(struct nsinfo *nsi)
236 {
237 	if (nsi && refcount_dec_and_test(nsinfo__refcnt(nsi)))
238 		nsinfo__delete(nsi);
239 	else
240 		RC_CHK_PUT(nsi);
241 }
242 
nsinfo__need_setns(const struct nsinfo * nsi)243 bool nsinfo__need_setns(const struct nsinfo *nsi)
244 {
245 	return RC_CHK_ACCESS(nsi)->need_setns;
246 }
247 
nsinfo__clear_need_setns(struct nsinfo * nsi)248 void nsinfo__clear_need_setns(struct nsinfo *nsi)
249 {
250 	RC_CHK_ACCESS(nsi)->need_setns = false;
251 }
252 
nsinfo__tgid(const struct nsinfo * nsi)253 pid_t nsinfo__tgid(const struct nsinfo  *nsi)
254 {
255 	return RC_CHK_ACCESS(nsi)->tgid;
256 }
257 
nsinfo__nstgid(const struct nsinfo * nsi)258 pid_t nsinfo__nstgid(const struct nsinfo  *nsi)
259 {
260 	return RC_CHK_ACCESS(nsi)->nstgid;
261 }
262 
nsinfo__pid(const struct nsinfo * nsi)263 pid_t nsinfo__pid(const struct nsinfo  *nsi)
264 {
265 	return RC_CHK_ACCESS(nsi)->pid;
266 }
267 
nsinfo__in_pidns(const struct nsinfo * nsi)268 bool nsinfo__in_pidns(const struct nsinfo *nsi)
269 {
270 	return RC_CHK_ACCESS(nsi)->in_pidns;
271 }
272 
nsinfo__set_in_pidns(struct nsinfo * nsi)273 void nsinfo__set_in_pidns(struct nsinfo *nsi)
274 {
275 	RC_CHK_ACCESS(nsi)->in_pidns = true;
276 }
277 
nsinfo__mountns_enter(struct nsinfo * nsi,struct nscookie * nc)278 void nsinfo__mountns_enter(struct nsinfo *nsi,
279 				  struct nscookie *nc)
280 {
281 	char curpath[PATH_MAX];
282 	int oldns = -1;
283 	int newns = -1;
284 	char *oldcwd = NULL;
285 
286 	if (nc == NULL)
287 		return;
288 
289 	nc->oldns = -1;
290 	nc->newns = -1;
291 
292 	if (!nsi || !nsinfo__need_setns(nsi))
293 		return;
294 
295 	if (!getcwd(curpath, sizeof(curpath)))
296 		return;
297 
298 	oldcwd = strdup(curpath);
299 	if (!oldcwd)
300 		return;
301 
302 	oldns = open("/proc/self/ns/mnt", O_RDONLY);
303 	if (oldns < 0)
304 		goto errout;
305 
306 	newns = open(nsinfo__mntns_path(nsi), O_RDONLY);
307 	if (newns < 0)
308 		goto errout;
309 
310 	if (setns(newns, CLONE_NEWNS) < 0)
311 		goto errout;
312 
313 	nc->oldcwd = oldcwd;
314 	nc->oldns = oldns;
315 	nc->newns = newns;
316 	return;
317 
318 errout:
319 	free(oldcwd);
320 	if (oldns > -1)
321 		close(oldns);
322 	if (newns > -1)
323 		close(newns);
324 }
325 
nsinfo__mountns_exit(struct nscookie * nc)326 void nsinfo__mountns_exit(struct nscookie *nc)
327 {
328 	if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd)
329 		return;
330 
331 	setns(nc->oldns, CLONE_NEWNS);
332 
333 	if (nc->oldcwd) {
334 		WARN_ON_ONCE(chdir(nc->oldcwd));
335 		zfree(&nc->oldcwd);
336 	}
337 
338 	if (nc->oldns > -1) {
339 		close(nc->oldns);
340 		nc->oldns = -1;
341 	}
342 
343 	if (nc->newns > -1) {
344 		close(nc->newns);
345 		nc->newns = -1;
346 	}
347 }
348 
nsinfo__realpath(const char * path,struct nsinfo * nsi)349 char *nsinfo__realpath(const char *path, struct nsinfo *nsi)
350 {
351 	char *rpath;
352 	struct nscookie nsc;
353 
354 	nsinfo__mountns_enter(nsi, &nsc);
355 	rpath = realpath(path, NULL);
356 	nsinfo__mountns_exit(&nsc);
357 
358 	return rpath;
359 }
360 
nsinfo__stat(const char * filename,struct stat * st,struct nsinfo * nsi)361 int nsinfo__stat(const char *filename, struct stat *st, struct nsinfo *nsi)
362 {
363 	int ret;
364 	struct nscookie nsc;
365 
366 	nsinfo__mountns_enter(nsi, &nsc);
367 	ret = stat(filename, st);
368 	nsinfo__mountns_exit(&nsc);
369 
370 	return ret;
371 }
372 
nsinfo__is_in_root_namespace(void)373 bool nsinfo__is_in_root_namespace(void)
374 {
375 	pid_t tgid = 0, nstgid = 0;
376 	bool in_pidns = false;
377 
378 	nsinfo__get_nspid(&tgid, &nstgid, &in_pidns, "/proc/self/status");
379 	return !in_pidns;
380 }
381