blob: 5942c21c15438e0c3ea91ea434c237b4886447be [file] [log] [blame]
Zeng Linggangb6b7bc42014-02-26 17:36:10 +08001/*
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 <fcntl.h>
28#include <unistd.h>
29#include <error.h>
30#include <stdlib.h>
31#include <errno.h>
32#include <string.h>
33#include <signal.h>
34#include "test.h"
Zeng Linggangb6b7bc42014-02-26 17:36:10 +080035#include "safe_macros.h"
36#include "fchownat.h"
37#include "lapi/fcntl.h"
38
39#define TESTFILE "testfile"
40#define TESTFILE_LINK "testfile_link"
41
42char *TCID = "fchownat02";
43int TST_TOTAL = 1;
44
45static int dirfd;
46static uid_t set_uid = 1000;
47static gid_t set_gid = 1000;
48static void setup(void);
49static void cleanup(void);
50static void test_verify(void);
51static void fchownat_verify(void);
52
53int main(int ac, char **av)
54{
55 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020056 const char *msg;
Zeng Linggangb6b7bc42014-02-26 17:36:10 +080057 int i;
58
59 msg = parse_opts(ac, av, NULL, NULL);
60 if (msg != NULL)
61 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
62
63 setup();
64
65 for (lc = 0; TEST_LOOPING(lc); lc++) {
66 tst_count = 0;
67 for (i = 0; i < TST_TOTAL; i++)
68 fchownat_verify();
69 }
70
71 cleanup();
72 tst_exit();
73}
74
75static void setup(void)
76{
77 struct stat c_buf, l_buf;
78
79 if ((tst_kvercmp(2, 6, 16)) < 0)
80 tst_brkm(TCONF, NULL, "This test needs kernel 2.6.16 or newer");
81
82 tst_require_root(NULL);
83
84 tst_sig(NOFORK, DEF_HANDLER, cleanup);
85
86 TEST_PAUSE;
87
88 tst_tmpdir();
89
90 dirfd = SAFE_OPEN(cleanup, "./", O_DIRECTORY);
91
92 SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL);
93
94 SAFE_SYMLINK(cleanup, TESTFILE, TESTFILE_LINK);
95
96 SAFE_STAT(cleanup, TESTFILE_LINK, &c_buf);
97
98 SAFE_LSTAT(cleanup, TESTFILE_LINK, &l_buf);
99
100 if (l_buf.st_uid == set_uid || l_buf.st_gid == set_gid) {
101 tst_brkm(TBROK | TERRNO, cleanup,
102 "link_uid(%d) == set_uid(%d) or link_gid(%d) == "
103 "set_gid(%d)", l_buf.st_uid, set_uid, l_buf.st_gid,
104 set_gid);
105 }
106}
107
108static void fchownat_verify(void)
109{
110 TEST(fchownat(dirfd, TESTFILE_LINK, set_uid, set_gid,
111 AT_SYMLINK_NOFOLLOW));
112
113 if (TEST_RETURN != 0) {
114 tst_resm(TFAIL | TTERRNO, "fchownat() failed, errno=%d : %s",
115 TEST_ERRNO, strerror(TEST_ERRNO));
116 } else {
117 test_verify();
118 }
119}
120
121static void test_verify(void)
122{
123 struct stat c_buf, l_buf;
124
125 SAFE_STAT(cleanup, TESTFILE_LINK, &c_buf);
126
127 SAFE_LSTAT(cleanup, TESTFILE_LINK, &l_buf);
128
129 if (c_buf.st_uid != set_uid && l_buf.st_uid == set_uid &&
130 c_buf.st_gid != set_gid && l_buf.st_gid == set_gid) {
131 tst_resm(TPASS, "fchownat() test AT_SYMLINK_NOFOLLOW success");
132 } else {
133 tst_resm(TFAIL,
134 "fchownat() test AT_SYMLINK_NOFOLLOW fail with uid=%d "
135 "link_uid=%d set_uid=%d | gid=%d link_gid=%d "
136 "set_gid=%d", c_buf.st_uid, l_buf.st_uid, set_uid,
137 c_buf.st_gid, l_buf.st_gid, set_gid);
138 }
139}
140
141static void cleanup(void)
142{
143 tst_rmdir();
Zeng Linggangb6b7bc42014-02-26 17:36:10 +0800144}