• 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 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/acpi/event", EAGAIN},
89 	{"read", "/proc/kmsg", EAGAIN},
90 	{"read", "/proc/sal/cpe/event", EAGAIN},
91 	{"read", "/proc/sal/cmc/event", EAGAIN},
92 	{"read", "/proc/sal/init/event", EAGAIN},
93 	{"read", "/proc/sal/mca/event", EAGAIN},
94 	{"read", "/proc/xen/privcmd", EIO},
95 	{"read", "/proc/xen/privcmd", EINVAL},
96 	{"read", "/proc/self/mem", EIO},
97 	{"read", "/proc/self/task/[0-9]*/mem", EIO},
98 	{"read", "/proc/self/attr/*", EINVAL},
99 	{"read", "/proc/self/task/[0-9]*/attr/*", EINVAL},
100 	{"read", "/proc/self/ns/*", EINVAL},
101 	{"read", "/proc/self/task/[0-9]*/ns/*", EINVAL},
102 	{"read", "/proc/ppc64/rtas/error_log", EINVAL},
103 	{"read", "/proc/powerpc/rtas/error_log", EINVAL},
104 	{"read", "/proc/fs/nfsd/unlock_filesystem", EINVAL},
105 	{"read", "/proc/fs/nfsd/unlock_ip", EINVAL},
106 	{"read", "/proc/fs/nfsd/filehandle", EINVAL},
107 	{"read", "/proc/fs/nfsd/.getfs", EINVAL},
108 	{"read", "/proc/fs/nfsd/.getfd", EINVAL},
109 	{"read", "/proc/self/net/rpc/use-gss-proxy", EAGAIN},
110 	{"read", "/proc/sys/net/ipv6/conf/*/stable_secret", EIO},
111 	{"read", "/proc/sys/vm/nr_hugepages", EOPNOTSUPP},
112 	{"read", "/proc/sys/vm/nr_overcommit_hugepages", EOPNOTSUPP},
113 	{"read", "/proc/sys/vm/nr_hugepages_mempolicy", EOPNOTSUPP},
114 	{"read", "/proc/debug/fwdump", EINVAL},
115 	{"read", "/proc/cid/athdiagpfs", EIO},
116 	{"", "", 0}
117 };
118 
119 /*
120  * If a particular LSM is enabled, it is expected that some entries can
121  * be read successfully. Otherwise, those entries will retrun some
122  * failures listed above. Here to add any LSM specific entries.
123  */
124 
125 /*
126  * Test macro to indicate that SELinux libraries and headers are
127  * installed.
128  */
129 #ifdef HAVE_LIBSELINUX_DEVEL
130 static const char lsm_should_work[][PATH_MAX] = {
131 	"/proc/self/attr/*",
132 	"/proc/self/task/[0-9]*/attr/*",
133 	""
134 };
135 
136 /* Place holder for none of LSM is detected. */
137 #else
138 static const char lsm_should_work[][PATH_MAX] = {
139 	""
140 };
141 #endif
142 
143 /* Known files that does not honor O_NONBLOCK, so they will hang
144    the test while being read. */
145 static const char error_nonblock[][PATH_MAX] = {
146 	"/proc/xen/xenbus",
147 	""
148 };
149 
150 /*
151  * Verify expected failures, and then let the test to continue.
152  *
153  * Return 0 when a problem errno is found.
154  * Return 1 when a known issue is found.
155  *
156  */
found_errno(const char * syscall,const char * obj,int tmperr)157 static int found_errno(const char *syscall, const char *obj, int tmperr)
158 {
159 	int i;
160 
161 	/* Should not see any error for certain entries if a LSM is enabled. */
162 #ifdef HAVE_LIBSELINUX_DEVEL
163 	if (is_selinux_enabled()) {
164 		for (i = 0; lsm_should_work[i][0] != '\0'; i++) {
165 			if (!strcmp(obj, lsm_should_work[i]) ||
166 			    !fnmatch(lsm_should_work[i], obj, FNM_PATHNAME)) {
167 				return 0;
168 			}
169 		}
170 	}
171 #endif
172 	for (i = 0; known_issues[i].err != 0; i++) {
173 		if (tmperr == known_issues[i].err &&
174 		    (!strcmp(obj, known_issues[i].file) ||
175 		     !fnmatch(known_issues[i].file, obj, FNM_PATHNAME)) &&
176 		    !strcmp(syscall, known_issues[i].func)) {
177 			/* Using strcmp / fnmatch could have messed up the
178 			 * errno value. */
179 			errno = tmperr;
180 			tst_resm(TINFO | TERRNO, "%s: known issue", obj);
181 			return 1;
182 		}
183 	}
184 	return 0;
185 }
186 
cleanup(void)187 static void cleanup(void)
188 {
189 	tst_rmdir();
190 }
191 
setup(void)192 static void setup(void)
193 {
194 	tst_sig(FORK, DEF_HANDLER, cleanup);
195 	TEST_PAUSE;
196 	tst_tmpdir();
197 }
198 
help(void)199 void help(void)
200 {
201 	printf("  -b x    read byte count\n");
202 	printf("  -m x    max megabytes to read from single file\n");
203 	printf("  -q      read .../irq/... entries\n");
204 	printf("  -r x    proc pathname\n");
205 	printf("  -v      verbose mode\n");
206 }
207 
208 /*
209  * add the -m option whose parameter is the
210  * pages that should be mapped.
211  */
212 static option_t options[] = {
213 	{"b:", &opt_buffsize, &opt_buffsizestr},
214 	{"m:", &opt_maxmbytes, &opt_maxmbytesstr},
215 	{"q", &opt_readirq, NULL},
216 	{"r:", &opt_procpath, &opt_procpathstr},
217 	{"v", &opt_verbose, NULL},
218 	{NULL, NULL, NULL}
219 };
220 
221 /*
222  * NB: this function is recursive
223  * returns 0 if no error encountered, otherwise number of errors (objs)
224  *
225  * REM: Funny enough, while developing this function (actually replacing
226  *	streamed fopen by standard open), I hit a real /proc bug.
227  *	On a 2.2.13-SuSE kernel, "cat /proc/tty/driver/serial" would fail
228  *	with EFAULT, while "cat /proc/tty/driver/serial > somefile" wouldn't.
229  *	Okay, this might be due to a slight serial misconfiguration, but still.
230  *	Analysis with strace showed up the difference was on the count size
231  *	of read (1024 bytes vs 4096 bytes). So I tested further..
232  *	read count of 512 bytes adds /proc/tty/drivers to the list
233  *	of broken proc files, while 64 bytes reads removes
234  *	/proc/tty/driver/serial from the list. Interesting, isn't it?
235  *	Now, there's a -b option to this test, so you can try your luck. --SF
236  *
237  * It's more fun to run this test it as root, as all the files will be accessible!
238  * (however, be careful, there might be some bufferoverflow holes..)
239  * reading proc files might be also a good kernel latency killer.
240  */
readproc(const char * obj)241 static long readproc(const char *obj)
242 {
243 	DIR *dir = NULL;	/* pointer to a directory */
244 	struct dirent *dir_ent;	/* pointer to directory entries */
245 	char dirobj[PATH_MAX];	/* object inside directory to modify */
246 	struct stat statbuf;	/* used to hold stat information */
247 	int fd, tmperr, i;
248 	ssize_t nread;
249 	static char buf[MAX_BUFF_SIZE];	/* static kills reentrancy, but we don't care about the contents */
250 	unsigned long long file_total_read = 0;
251 
252 	/* Determine the file type */
253 	if (lstat(obj, &statbuf) < 0) {
254 
255 		/* permission denied is not considered as error */
256 		if (errno != EACCES) {
257 			tst_resm(TFAIL | TERRNO, "%s: lstat", obj);
258 			return 1;
259 		}
260 		return 0;
261 
262 	}
263 
264 	/* Prevent loops, but read /proc/self. */
265 	if (S_ISLNK(statbuf.st_mode) && strcmp(obj, selfpath))
266 		return 0;
267 
268 	total_obj++;
269 
270 	/* Take appropriate action, depending on the file type */
271 	if (S_ISDIR(statbuf.st_mode) || !strcmp(obj, selfpath)) {
272 
273 		/* object is a directory */
274 
275 		/*
276 		 * Skip over the /proc/irq directory, unless the user
277 		 * requested that we read the directory because it could
278 		 * map to a broken driver which effectively `hangs' the
279 		 * test.
280 		 */
281 		if (!opt_readirq && !strcmp("/proc/irq", obj)) {
282 			return 0;
283 			/* Open the directory to get access to what is in it */
284 		} else if ((dir = opendir(obj)) == NULL) {
285 			if (errno != EACCES) {
286 				tst_resm(TFAIL | TERRNO, "%s: opendir", obj);
287 				return 1;
288 			}
289 			return 0;
290 		} else {
291 
292 			long ret_val = 0;
293 
294 			/* Loop through the entries in the directory */
295 			for (dir_ent = (struct dirent *)readdir(dir);
296 			     dir_ent != NULL;
297 			     dir_ent = (struct dirent *)readdir(dir)) {
298 
299 				/* Ignore ".", "..", "kcore", and
300 				 * "/proc/<pid>" (unless this is our
301 				 * starting point as directed by the
302 				 * user).
303 				 */
304 				if (strcmp(dir_ent->d_name, ".") &&
305 				    strcmp(dir_ent->d_name, "..") &&
306 				    strcmp(dir_ent->d_name, "kcore") &&
307 				    (fnmatch("[0-9]*", dir_ent->d_name,
308 					     FNM_PATHNAME) ||
309 				     strcmp(obj, procpath))) {
310 
311 					if (opt_verbose) {
312 						fprintf(stderr, "%s\n",
313 							dir_ent->d_name);
314 					}
315 
316 					/* Recursively call this routine to test the
317 					 * current entry */
318 					snprintf(dirobj, PATH_MAX,
319 						 "%s/%s", obj, dir_ent->d_name);
320 					ret_val += readproc(dirobj);
321 
322 				}
323 
324 			}
325 
326 			/* Close the directory */
327 			if (dir)
328 				(void)closedir(dir);
329 
330 			return ret_val;
331 
332 		}
333 
334 	} else {		/* if it's not a dir, read it! */
335 
336 		if (!S_ISREG(statbuf.st_mode))
337 			return 0;
338 
339 #ifdef DEBUG
340 		fprintf(stderr, "%s", obj);
341 #endif
342 
343 		/* is O_NONBLOCK enough to escape from FIFO's ? */
344 		fd = open(obj, O_RDONLY | O_NONBLOCK);
345 		if (fd < 0) {
346 			tmperr = errno;
347 
348 			if (!found_errno("open", obj, tmperr)) {
349 
350 				errno = tmperr;
351 
352 				if (errno != EACCES) {
353 					tst_resm(TFAIL | TERRNO,
354 						 "%s: open failed", obj);
355 					return 1;
356 				}
357 
358 			}
359 			return 0;
360 
361 		}
362 
363 		/* Skip write-only files. */
364 		if ((statbuf.st_mode & S_IRUSR) == 0 &&
365 		    (statbuf.st_mode & S_IWUSR) != 0) {
366 			tst_resm(TINFO, "%s: is write-only.", obj);
367 			return 0;
368 		}
369 
370 		/* Skip files does not honor O_NONBLOCK. */
371 		for (i = 0; error_nonblock[i][0] != '\0'; i++) {
372 			if (!strcmp(obj, error_nonblock[i])) {
373 				tst_resm(TINFO, "%s: does not honor "
374 					 "O_NONBLOCK", obj);
375 				return 0;
376 			}
377 		}
378 
379 		file_total_read = 0;
380 		do {
381 
382 			nread = read(fd, buf, buffsize);
383 
384 			if (nread < 0) {
385 
386 				tmperr = errno;
387 				(void)close(fd);
388 
389 				/* ignore no perm (not root) and no
390 				 * process (terminated) errors */
391 				if (!found_errno("read", obj, tmperr)) {
392 
393 					errno = tmperr;
394 
395 					if (errno != EACCES && errno != ESRCH) {
396 						tst_resm(TFAIL | TERRNO,
397 							 "read failed: "
398 							 "%s", obj);
399 						return 1;
400 					}
401 					return 0;
402 
403 				}
404 
405 			} else
406 				file_total_read += nread;
407 
408 			if (opt_verbose) {
409 #ifdef DEBUG
410 				fprintf(stderr, "%ld", nread);
411 #endif
412 				fprintf(stderr, ".");
413 			}
414 
415 			if ((maxbytes > 0) && (file_total_read > maxbytes)) {
416 				tst_resm(TINFO, "%s: reached maxmbytes (-m)",
417 					 obj);
418 				break;
419 			}
420 		} while (0 < nread);
421 		total_read += file_total_read;
422 
423 		if (opt_verbose)
424 			fprintf(stderr, "\n");
425 
426 		if (0 <= fd)
427 			(void)close(fd);
428 
429 	}
430 
431 	/* It's better to assume success by default rather than failure. */
432 	return 0;
433 
434 }
435 
main(int argc,char * argv[])436 int main(int argc, char *argv[])
437 {
438 	int lc;
439 
440 	tst_parse_opts(argc, argv, options, help);
441 
442 	if (opt_buffsize) {
443 		size_t bs;
444 		bs = atoi(opt_buffsizestr);
445 		if (bs <= MAX_BUFF_SIZE)
446 			buffsize = bs;
447 		else
448 			tst_brkm(TBROK, cleanup,
449 				 "Invalid arg for -b (max: %u): %s",
450 				 MAX_BUFF_SIZE, opt_buffsizestr);
451 	}
452 	if (opt_maxmbytes)
453 		maxbytes = atoi(opt_maxmbytesstr) * 1024 * 1024;
454 
455 	if (opt_procpath)
456 		procpath = opt_procpathstr;
457 
458 	setup();
459 
460 	for (lc = 0; TEST_LOOPING(lc); lc++) {
461 		tst_count = 0;
462 
463 		TEST(readproc(procpath));
464 
465 		if (TEST_RETURN != 0) {
466 			tst_resm(TFAIL, "readproc() failed with %ld errors.",
467 				 TEST_RETURN);
468 		} else {
469 			tst_resm(TPASS, "readproc() completed successfully, "
470 				 "total read: %llu bytes, %u objs", total_read,
471 				 total_obj);
472 		}
473 	}
474 
475 	cleanup();
476 	tst_exit();
477 }
478