• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * proc01.c - Tests Linux /proc file reading.
3  *
4  * Copyright (C) 2001 Stephane Fillod <f4cfe@free.fr>
5  * Copyright (c) 2008, 2009  Red Hat, Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it would be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  *
15  * Further, this software is distributed without any warranty that it is
16  * free of the rightful claim of any third person regarding infringement
17  * or the like.  Any license provided herein, whether implied or
18  * otherwise, applies only to this software file.  Patent licenses, if
19  * any, provided herein do not apply to combinations of this program with
20  * other software, or any other product whatsoever.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, write the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  *
26  */
27 
28 #include "config.h"
29 
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <dirent.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <fnmatch.h>
41 
42 #ifdef HAVE_LIBSELINUX_DEVEL
43 #include <selinux/selinux.h>
44 #endif
45 
46 #include "test.h"
47 
48 #define MAX_BUFF_SIZE 65536
49 #define MAX_FUNC_NAME 256
50 
51 char *TCID = "proc01";
52 int TST_TOTAL = 1;
53 
54 static int opt_verbose;
55 static int opt_procpath;
56 static char *opt_procpathstr;
57 static int opt_buffsize;
58 static int opt_readirq;
59 static char *opt_buffsizestr;
60 static int opt_maxmbytes;
61 static char *opt_maxmbytesstr;
62 
63 static char *procpath = "/proc";
64 static const char selfpath[] = "/proc/self";
65 size_t buffsize = 1024;
66 static unsigned long long maxbytes;
67 
68 unsigned long long total_read;
69 unsigned int total_obj;
70 
71 struct mapping {
72 	char func[MAX_FUNC_NAME];
73 	char file[PATH_MAX];
74 	int err;
75 };
76 
77 /* Those are known failures for 2.6.18 baremetal kernel and Xen dom0
78    kernel on i686, x86_64, ia64, ppc64 and s390x. In addition, It looks
79    like if SELinux is disabled, the test may still fail on some other
80    entries. */
81 static const struct mapping known_issues[] = {
82 	{"open", "/proc/acpi/event", EBUSY},
83 	{"open", "/proc/sal/cpe/data", EBUSY},
84 	{"open", "/proc/sal/cmc/data", EBUSY},
85 	{"open", "/proc/sal/init/data", EBUSY},
86 	{"open", "/proc/sal/mca/data", EBUSY},
87 	{"open", "/proc/fs/nfsd/pool_stats", ENODEV},
88 	{"read", "/proc/fs/nfsd/clients/*/ctl", EINVAL},
89 	{"read", "/proc/acpi/event", EAGAIN},
90 	{"read", "/proc/kmsg", EAGAIN},
91 	{"read", "/proc/sal/cpe/event", EAGAIN},
92 	{"read", "/proc/sal/cmc/event", EAGAIN},
93 	{"read", "/proc/sal/init/event", EAGAIN},
94 	{"read", "/proc/sal/mca/event", EAGAIN},
95 	{"read", "/proc/xen/privcmd", EIO},
96 	{"read", "/proc/xen/privcmd", EINVAL},
97 	{"read", "/proc/self/mem", EIO},
98 	{"read", "/proc/self/task/[0-9]*/mem", EIO},
99 	{"read", "/proc/self/attr/*", EINVAL},
100 	{"read", "/proc/self/attr/selinux/*", EINVAL},
101 	{"read", "/proc/self/attr/smack/*", EINVAL},
102 	{"read", "/proc/self/attr/apparmor/*", EINVAL},
103 	{"read", "/proc/self/task/[0-9]*/attr/*", EINVAL},
104 	{"read", "/proc/self/task/[0-9]*/attr/smack/*", EINVAL},
105 	{"read", "/proc/self/task/[0-9]*/attr/selinux/*", EINVAL},
106 	{"read", "/proc/self/task/[0-9]*/attr/apparmor/*", EINVAL},
107 	{"read", "/proc/self/ns/*", EINVAL},
108 	{"read", "/proc/self/task/[0-9]*/ns/*", EINVAL},
109 	{"read", "/proc/ppc64/rtas/error_log", EINVAL},
110 	{"read", "/proc/powerpc/rtas/error_log", EINVAL},
111 	{"read", "/proc/fs/nfsd/unlock_filesystem", EINVAL},
112 	{"read", "/proc/fs/nfsd/unlock_ip", EINVAL},
113 	{"read", "/proc/fs/nfsd/filehandle", EINVAL},
114 	{"read", "/proc/fs/nfsd/.getfs", EINVAL},
115 	{"read", "/proc/fs/nfsd/.getfd", EINVAL},
116 	{"read", "/proc/self/net/rpc/use-gss-proxy", EAGAIN},
117 	{"read", "/proc/sys/net/ipv6/conf/*/stable_secret", EIO},
118 	{"read", "/proc/sys/vm/nr_hugepages", EOPNOTSUPP},
119 	{"read", "/proc/sys/vm/nr_overcommit_hugepages", EOPNOTSUPP},
120 	{"read", "/proc/sys/vm/nr_hugepages_mempolicy", EOPNOTSUPP},
121 	{"read", "/proc/pressure/*", EOPNOTSUPP},
122 	{"", "", 0}
123 };
124 
125 /*
126  * If a particular LSM is enabled, it is expected that some entries can
127  * be read successfully. Otherwise, those entries will retrun some
128  * failures listed above. Here to add any LSM specific entries.
129  */
130 
131 /*
132  * Test macro to indicate that SELinux libraries and headers are
133  * installed.
134  */
135 #ifdef HAVE_LIBSELINUX_DEVEL
136 static const char lsm_should_work[][PATH_MAX] = {
137 	"/proc/self/attr/*",
138 	"/proc/self/attr/selinux/*",
139 	"/proc/self/task/[0-9]*/attr/*",
140 	"/proc/self/task/[0-9]*/attr/selinux/*",
141 	""
142 };
143 #endif
144 
145 /* Known files that does not honor O_NONBLOCK, so they will hang
146    the test while being read. */
147 static const char error_nonblock[][PATH_MAX] = {
148 	"/proc/xen/xenbus",
149 	""
150 };
151 
152 /*
153  * Verify expected failures, and then let the test to continue.
154  *
155  * Return 0 when a problem errno is found.
156  * Return 1 when a known issue is found.
157  *
158  */
found_errno(const char * syscall,const char * obj,int tmperr)159 static int found_errno(const char *syscall, const char *obj, int tmperr)
160 {
161 	int i;
162 
163 	/* Should not see any error for certain entries if a LSM is enabled. */
164 #ifdef HAVE_LIBSELINUX_DEVEL
165 	if (is_selinux_enabled()) {
166 		for (i = 0; lsm_should_work[i][0] != '\0'; i++) {
167 			if (!strcmp(obj, lsm_should_work[i]) ||
168 			    !fnmatch(lsm_should_work[i], obj, FNM_PATHNAME)) {
169 				return 0;
170 			}
171 		}
172 	}
173 #endif
174 	for (i = 0; known_issues[i].err != 0; i++) {
175 		if (tmperr == known_issues[i].err &&
176 		    (!strcmp(obj, known_issues[i].file) ||
177 		     !fnmatch(known_issues[i].file, obj, FNM_PATHNAME)) &&
178 		    !strcmp(syscall, known_issues[i].func)) {
179 			/* Using strcmp / fnmatch could have messed up the
180 			 * errno value. */
181 			errno = tmperr;
182 			tst_resm(TINFO | TERRNO, "%s: known issue", obj);
183 			return 1;
184 		}
185 	}
186 	return 0;
187 }
188 
cleanup(void)189 static void cleanup(void)
190 {
191 	tst_rmdir();
192 }
193 
setup(void)194 static void setup(void)
195 {
196 	tst_sig(FORK, DEF_HANDLER, cleanup);
197 	TEST_PAUSE;
198 	tst_tmpdir();
199 }
200 
help(void)201 void help(void)
202 {
203 	printf("  -b x    read byte count\n");
204 	printf("  -m x    max megabytes to read from single file\n");
205 	printf("  -q      read .../irq/... entries\n");
206 	printf("  -r x    proc pathname\n");
207 	printf("  -v      verbose mode\n");
208 }
209 
210 /*
211  * add the -m option whose parameter is the
212  * pages that should be mapped.
213  */
214 static option_t options[] = {
215 	{"b:", &opt_buffsize, &opt_buffsizestr},
216 	{"m:", &opt_maxmbytes, &opt_maxmbytesstr},
217 	{"q", &opt_readirq, NULL},
218 	{"r:", &opt_procpath, &opt_procpathstr},
219 	{"v", &opt_verbose, NULL},
220 	{NULL, NULL, NULL}
221 };
222 
223 /*
224  * NB: this function is recursive
225  * returns 0 if no error encountered, otherwise number of errors (objs)
226  *
227  * REM: Funny enough, while developing this function (actually replacing
228  *	streamed fopen by standard open), I hit a real /proc bug.
229  *	On a 2.2.13-SuSE kernel, "cat /proc/tty/driver/serial" would fail
230  *	with EFAULT, while "cat /proc/tty/driver/serial > somefile" wouldn't.
231  *	Okay, this might be due to a slight serial misconfiguration, but still.
232  *	Analysis with strace showed up the difference was on the count size
233  *	of read (1024 bytes vs 4096 bytes). So I tested further..
234  *	read count of 512 bytes adds /proc/tty/drivers to the list
235  *	of broken proc files, while 64 bytes reads removes
236  *	/proc/tty/driver/serial from the list. Interesting, isn't it?
237  *	Now, there's a -b option to this test, so you can try your luck. --SF
238  *
239  * It's more fun to run this test it as root, as all the files will be accessible!
240  * (however, be careful, there might be some bufferoverflow holes..)
241  * reading proc files might be also a good kernel latency killer.
242  */
readproc(const char * obj)243 static long readproc(const char *obj)
244 {
245 	DIR *dir = NULL;	/* pointer to a directory */
246 	struct dirent *dir_ent;	/* pointer to directory entries */
247 	char dirobj[PATH_MAX];	/* object inside directory to modify */
248 	struct stat statbuf;	/* used to hold stat information */
249 	int fd, tmperr, i;
250 	ssize_t nread;
251 	static char buf[MAX_BUFF_SIZE];	/* static kills reentrancy, but we don't care about the contents */
252 	unsigned long long file_total_read = 0;
253 
254 	/* Determine the file type */
255 	if (lstat(obj, &statbuf) < 0) {
256 
257 		/* permission denied is not considered as error */
258 		if (errno != EACCES) {
259 			tst_resm(TFAIL | TERRNO, "%s: lstat", obj);
260 			return 1;
261 		}
262 		return 0;
263 
264 	}
265 
266 	/* Prevent loops, but read /proc/self. */
267 	if (S_ISLNK(statbuf.st_mode) && strcmp(obj, selfpath))
268 		return 0;
269 
270 	total_obj++;
271 
272 	/* Take appropriate action, depending on the file type */
273 	if (S_ISDIR(statbuf.st_mode) || !strcmp(obj, selfpath)) {
274 
275 		/* object is a directory */
276 
277 		/*
278 		 * Skip over the /proc/irq directory, unless the user
279 		 * requested that we read the directory because it could
280 		 * map to a broken driver which effectively `hangs' the
281 		 * test.
282 		 */
283 		if (!opt_readirq && !strcmp("/proc/irq", obj)) {
284 			return 0;
285 			/* Open the directory to get access to what is in it */
286 		} else if ((dir = opendir(obj)) == NULL) {
287 			if (errno != EACCES) {
288 				tst_resm(TFAIL | TERRNO, "%s: opendir", obj);
289 				return 1;
290 			}
291 			return 0;
292 		} else {
293 
294 			long ret_val = 0;
295 
296 			/* Loop through the entries in the directory */
297 			for (dir_ent = (struct dirent *)readdir(dir);
298 			     dir_ent != NULL;
299 			     dir_ent = (struct dirent *)readdir(dir)) {
300 
301 				/* Ignore ".", "..", "kcore", and
302 				 * "/proc/<pid>" (unless this is our
303 				 * starting point as directed by the
304 				 * user).
305 				 */
306 				if (strcmp(dir_ent->d_name, ".") &&
307 				    strcmp(dir_ent->d_name, "..") &&
308 				    strcmp(dir_ent->d_name, "kcore") &&
309 				    (fnmatch("[0-9]*", dir_ent->d_name,
310 					     FNM_PATHNAME) ||
311 				     strcmp(obj, procpath))) {
312 
313 					if (opt_verbose) {
314 						fprintf(stderr, "%s\n",
315 							dir_ent->d_name);
316 					}
317 
318 					/* Recursively call this routine to test the
319 					 * current entry */
320 					snprintf(dirobj, PATH_MAX,
321 						 "%s/%s", obj, dir_ent->d_name);
322 					ret_val += readproc(dirobj);
323 
324 				}
325 
326 			}
327 
328 			/* Close the directory */
329 			if (dir)
330 				(void)closedir(dir);
331 
332 			return ret_val;
333 
334 		}
335 
336 	} else {		/* if it's not a dir, read it! */
337 
338 		if (!S_ISREG(statbuf.st_mode))
339 			return 0;
340 
341 #ifdef DEBUG
342 		fprintf(stderr, "%s", obj);
343 #endif
344 
345 		/* is O_NONBLOCK enough to escape from FIFO's ? */
346 		fd = open(obj, O_RDONLY | O_NONBLOCK);
347 		if (fd < 0) {
348 			tmperr = errno;
349 
350 			if (!found_errno("open", obj, tmperr)) {
351 
352 				errno = tmperr;
353 
354 				if (errno != EACCES) {
355 					tst_resm(TFAIL | TERRNO,
356 						 "%s: open failed", obj);
357 					return 1;
358 				}
359 
360 			}
361 			return 0;
362 
363 		}
364 
365 		/* Skip write-only files. */
366 		if ((statbuf.st_mode & S_IRUSR) == 0 &&
367 		    (statbuf.st_mode & S_IWUSR) != 0) {
368 			tst_resm(TINFO, "%s: is write-only.", obj);
369 			(void)close(fd);
370 			return 0;
371 		}
372 
373 		/* Skip files does not honor O_NONBLOCK. */
374 		for (i = 0; error_nonblock[i][0] != '\0'; i++) {
375 			if (!strcmp(obj, error_nonblock[i])) {
376 				tst_resm(TINFO, "%s: does not honor "
377 					 "O_NONBLOCK", obj);
378 				(void)close(fd);
379 				return 0;
380 			}
381 		}
382 
383 		file_total_read = 0;
384 		do {
385 
386 			nread = read(fd, buf, buffsize);
387 
388 			if (nread < 0) {
389 
390 				tmperr = errno;
391 				(void)close(fd);
392 
393 				/* ignore no perm (not root) and no
394 				 * process (terminated) errors */
395 				if (!found_errno("read", obj, tmperr)) {
396 
397 					errno = tmperr;
398 
399 					if (errno != EACCES && errno != ESRCH) {
400 						tst_resm(TFAIL | TERRNO,
401 							 "read failed: "
402 							 "%s", obj);
403 						return 1;
404 					}
405 					return 0;
406 
407 				}
408 
409 			} else
410 				file_total_read += nread;
411 
412 			if (opt_verbose) {
413 #ifdef DEBUG
414 				fprintf(stderr, "%ld", nread);
415 #endif
416 				fprintf(stderr, ".");
417 			}
418 
419 			if ((maxbytes > 0) && (file_total_read > maxbytes)) {
420 				tst_resm(TINFO, "%s: reached maxmbytes (-m)",
421 					 obj);
422 				break;
423 			}
424 		} while (0 < nread);
425 		total_read += file_total_read;
426 
427 		if (opt_verbose)
428 			fprintf(stderr, "\n");
429 
430 		if (0 <= fd)
431 			(void)close(fd);
432 
433 	}
434 
435 	/* It's better to assume success by default rather than failure. */
436 	return 0;
437 
438 }
439 
main(int argc,char * argv[])440 int main(int argc, char *argv[])
441 {
442 	int lc;
443 
444 	tst_parse_opts(argc, argv, options, help);
445 
446 	if (opt_buffsize) {
447 		size_t bs;
448 		bs = atoi(opt_buffsizestr);
449 		if (bs <= MAX_BUFF_SIZE)
450 			buffsize = bs;
451 		else
452 			tst_brkm(TBROK, cleanup,
453 				 "Invalid arg for -b (max: %u): %s",
454 				 MAX_BUFF_SIZE, opt_buffsizestr);
455 	}
456 	if (opt_maxmbytes)
457 		maxbytes = atoi(opt_maxmbytesstr) * 1024 * 1024;
458 
459 	if (opt_procpath)
460 		procpath = opt_procpathstr;
461 
462 	setup();
463 
464 	for (lc = 0; TEST_LOOPING(lc); lc++) {
465 		tst_count = 0;
466 
467 		TEST(readproc(procpath));
468 
469 		if (TEST_RETURN != 0) {
470 			tst_resm(TFAIL, "readproc() failed with %ld errors.",
471 				 TEST_RETURN);
472 		} else {
473 			tst_resm(TPASS, "readproc() completed successfully, "
474 				 "total read: %llu bytes, %u objs", total_read,
475 				 total_obj);
476 		}
477 	}
478 
479 	cleanup();
480 	tst_exit();
481 }
482