1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2017 Fujitsu Ltd.
4 * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
5 */
6
7 /*
8 * Description:
9 * fcntl(2) manpage states that an unprivileged user could not set the
10 * pipe capacity above the limit in /proc/sys/fs/pipe-max-size. However,
11 * an unprivileged user could create a pipe whose initial capacity exceeds
12 * the limit. We add a regression test to check that pipe-max-size caps
13 * the initial allocation for a new pipe for unprivileged users, but not
14 * for privileged users.
15 *
16 * This kernel bug has been fixed by:
17 *
18 * commit 086e774a57fba4695f14383c0818994c0b31da7c
19 * Author: Michael Kerrisk (man-pages) <mtk.manpages@gmail.com>
20 * Date: Tue Oct 11 13:53:43 2016 -0700
21 *
22 * pipe: cap initial pipe capacity according to pipe-max-size limit
23 */
24
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <pwd.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <stdlib.h>
31
32 #include "lapi/fcntl.h"
33 #include "tst_test.h"
34
35 static int pipe_max_unpriv;
36 static int test_max_unpriv;
37 static int test_max_priv;
38 static struct passwd *pw;
39 static struct tcase {
40 int *exp_sz;
41 int exp_usr;
42 char *des;
43 } tcases[] = {
44 {&test_max_unpriv, 1, "an unprivileged user"},
45 {&test_max_priv, 0, "a privileged user"}
46 };
47
setup(void)48 static void setup(void)
49 {
50 test_max_unpriv = getpagesize();
51 test_max_priv = test_max_unpriv * 16;
52
53 if (!access("/proc/sys/fs/pipe-max-size", F_OK)) {
54 SAFE_FILE_SCANF("/proc/sys/fs/pipe-max-size", "%d",
55 &pipe_max_unpriv);
56 SAFE_FILE_PRINTF("/proc/sys/fs/pipe-max-size", "%d",
57 test_max_unpriv);
58 } else {
59 tst_brk(TCONF, "/proc/sys/fs/pipe-max-size doesn't exist");
60 }
61
62 pw = SAFE_GETPWNAM("nobody");
63 }
64
cleanup(void)65 static void cleanup(void)
66 {
67 SAFE_FILE_PRINTF("/proc/sys/fs/pipe-max-size", "%d", pipe_max_unpriv);
68 }
69
verify_pipe_size(int exp_pip_sz,char * desp)70 static int verify_pipe_size(int exp_pip_sz, char *desp)
71 {
72 int get_size;
73 int fds[2];
74
75 SAFE_PIPE(fds);
76
77 get_size = fcntl(fds[1], F_GETPIPE_SZ);
78 if (get_size == -1) {
79 tst_res(TFAIL | TERRNO, "fcntl(2) with F_GETPIPE_SZ failed");
80 goto end;
81 }
82
83 if (get_size != exp_pip_sz) {
84 tst_res(TFAIL, "%s init the capacity of a pipe to %d "
85 "unexpectedly, expected %d", desp, get_size,
86 exp_pip_sz);
87 } else {
88 tst_res(TPASS, "%s init the capacity of a pipe to %d "
89 "successfully", desp, exp_pip_sz);
90 }
91
92 end:
93 if (fds[0] > 0)
94 SAFE_CLOSE(fds[0]);
95
96 if (fds[1] > 0)
97 SAFE_CLOSE(fds[1]);
98
99 exit(0);
100 }
101
do_test(unsigned int n)102 static void do_test(unsigned int n)
103 {
104 struct tcase *tc = &tcases[n];
105
106 if (!SAFE_FORK()) {
107 if (tc->exp_usr)
108 SAFE_SETUID(pw->pw_uid);
109
110 verify_pipe_size(*tc->exp_sz, tc->des);
111 }
112
113 tst_reap_children();
114 }
115
116 static struct tst_test test = {
117 .min_kver = "2.6.35",
118 .needs_root = 1,
119 .forks_child = 1,
120 .tcnt = ARRAY_SIZE(tcases),
121 .setup = setup,
122 .cleanup = cleanup,
123 .test = do_test,
124 .tags = (const struct tst_tag[]) {
125 {"linux-git", "086e774a57fb"},
126 {}
127 }
128 };
129