1 /*
2 * Copyright (c) 2014 Fujitsu Ltd.
3 * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program.
15 */
16 /*
17 * Test Description:
18 * Verify that,
19 * The flag of fchownat() is AT_SYMLINK_NOFOLLOW and the pathname would
20 * not be dereferenced if the pathname is a symbolic link.
21 */
22
23 #define _GNU_SOURCE
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <signal.h>
32 #include "test.h"
33 #include "safe_macros.h"
34 #include "fchownat.h"
35 #include "lapi/fcntl.h"
36
37 #define TESTFILE "testfile"
38 #define TESTFILE_LINK "testfile_link"
39
40 char *TCID = "fchownat02";
41 int TST_TOTAL = 1;
42
43 static int dir_fd;
44 static uid_t set_uid = 1000;
45 static gid_t set_gid = 1000;
46 static void setup(void);
47 static void cleanup(void);
48 static void test_verify(void);
49 static void fchownat_verify(void);
50
main(int ac,char ** av)51 int main(int ac, char **av)
52 {
53 int lc;
54 int i;
55
56 tst_parse_opts(ac, av, NULL, NULL);
57
58 setup();
59
60 for (lc = 0; TEST_LOOPING(lc); lc++) {
61 tst_count = 0;
62 for (i = 0; i < TST_TOTAL; i++)
63 fchownat_verify();
64 }
65
66 cleanup();
67 tst_exit();
68 }
69
setup(void)70 static void setup(void)
71 {
72 struct stat c_buf, l_buf;
73
74 if ((tst_kvercmp(2, 6, 16)) < 0)
75 tst_brkm(TCONF, NULL, "This test needs kernel 2.6.16 or newer");
76
77 tst_require_root();
78
79 tst_sig(NOFORK, DEF_HANDLER, cleanup);
80
81 TEST_PAUSE;
82
83 tst_tmpdir();
84
85 dir_fd = SAFE_OPEN(cleanup, "./", O_DIRECTORY);
86
87 SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL);
88
89 SAFE_SYMLINK(cleanup, TESTFILE, TESTFILE_LINK);
90
91 SAFE_STAT(cleanup, TESTFILE_LINK, &c_buf);
92
93 SAFE_LSTAT(cleanup, TESTFILE_LINK, &l_buf);
94
95 if (l_buf.st_uid == set_uid || l_buf.st_gid == set_gid) {
96 tst_brkm(TBROK | TERRNO, cleanup,
97 "link_uid(%d) == set_uid(%d) or link_gid(%d) == "
98 "set_gid(%d)", l_buf.st_uid, set_uid, l_buf.st_gid,
99 set_gid);
100 }
101 }
102
fchownat_verify(void)103 static void fchownat_verify(void)
104 {
105 TEST(fchownat(dir_fd, TESTFILE_LINK, set_uid, set_gid,
106 AT_SYMLINK_NOFOLLOW));
107
108 if (TEST_RETURN != 0) {
109 tst_resm(TFAIL | TTERRNO, "fchownat() failed, errno=%d : %s",
110 TEST_ERRNO, strerror(TEST_ERRNO));
111 } else {
112 test_verify();
113 }
114 }
115
test_verify(void)116 static void test_verify(void)
117 {
118 struct stat c_buf, l_buf;
119
120 SAFE_STAT(cleanup, TESTFILE_LINK, &c_buf);
121
122 SAFE_LSTAT(cleanup, TESTFILE_LINK, &l_buf);
123
124 if (c_buf.st_uid != set_uid && l_buf.st_uid == set_uid &&
125 c_buf.st_gid != set_gid && l_buf.st_gid == set_gid) {
126 tst_resm(TPASS, "fchownat() test AT_SYMLINK_NOFOLLOW success");
127 } else {
128 tst_resm(TFAIL,
129 "fchownat() test AT_SYMLINK_NOFOLLOW fail with uid=%d "
130 "link_uid=%d set_uid=%d | gid=%d link_gid=%d "
131 "set_gid=%d", c_buf.st_uid, l_buf.st_uid, set_uid,
132 c_buf.st_gid, l_buf.st_gid, set_gid);
133 }
134 }
135
cleanup(void)136 static void cleanup(void)
137 {
138 tst_rmdir();
139 }
140