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