• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdlib.h>
30 
31 #include "lapi/fcntl.h"
32 #include "tst_test.h"
33 
34 static int pipe_max_unpriv;
35 static int test_max_unpriv;
36 static int test_max_priv;
37 static struct passwd *pw;
38 static struct tcase {
39 	int *exp_sz;
40 	int exp_usr;
41 	char *des;
42 } tcases[] = {
43 	{&test_max_unpriv, 1, "an unprivileged user"},
44 	{&test_max_priv, 0, "a privileged user"}
45 };
46 
setup(void)47 static void setup(void)
48 {
49 	test_max_unpriv = getpagesize();
50 	test_max_priv = test_max_unpriv * 16;
51 
52 	if (!access("/proc/sys/fs/pipe-max-size", F_OK)) {
53 		SAFE_FILE_SCANF("/proc/sys/fs/pipe-max-size", "%d",
54 				&pipe_max_unpriv);
55 		SAFE_FILE_PRINTF("/proc/sys/fs/pipe-max-size", "%d",
56 				test_max_unpriv);
57 	} else {
58 		tst_brk(TCONF, "/proc/sys/fs/pipe-max-size doesn't exist");
59 	}
60 
61 	pw = SAFE_GETPWNAM("nobody");
62 }
63 
cleanup(void)64 static void cleanup(void)
65 {
66 	SAFE_FILE_PRINTF("/proc/sys/fs/pipe-max-size", "%d", pipe_max_unpriv);
67 }
68 
verify_pipe_size(int exp_pip_sz,char * desp)69 static int verify_pipe_size(int exp_pip_sz, char *desp)
70 {
71 	int get_size;
72 	int fds[2];
73 
74 	SAFE_PIPE(fds);
75 
76 	get_size = fcntl(fds[1], F_GETPIPE_SZ);
77 	if (get_size == -1) {
78 		tst_res(TFAIL | TERRNO, "fcntl(2) with F_GETPIPE_SZ failed");
79 		goto end;
80 	}
81 
82 	if (get_size != exp_pip_sz) {
83 		tst_res(TFAIL, "%s init the capacity of a pipe to %d "
84 			"unexpectedly, expected %d", desp, get_size,
85 			exp_pip_sz);
86 	} else {
87 		tst_res(TPASS, "%s init the capacity of a pipe to %d "
88 			"successfully", desp, exp_pip_sz);
89 	}
90 
91 end:
92 	if (fds[0] > 0)
93 		SAFE_CLOSE(fds[0]);
94 
95 	if (fds[1] > 0)
96 		SAFE_CLOSE(fds[1]);
97 
98 	exit(0);
99 }
100 
do_test(unsigned int n)101 static void do_test(unsigned int n)
102 {
103 	struct tcase *tc = &tcases[n];
104 
105 	if (!SAFE_FORK()) {
106 		if (tc->exp_usr)
107 			SAFE_SETUID(pw->pw_uid);
108 
109 		verify_pipe_size(*tc->exp_sz, tc->des);
110 	}
111 
112 	tst_reap_children();
113 }
114 
115 static struct tst_test test = {
116 	.min_kver = "2.6.35",
117 	.needs_root = 1,
118 	.forks_child = 1,
119 	.tcnt = ARRAY_SIZE(tcases),
120 	.setup = setup,
121 	.cleanup = cleanup,
122 	.test = do_test,
123 	.tags = (const struct tst_tag[]) {
124 		{"linux-git", "086e774a57fb"},
125 		{}
126 	}
127 };
128