blob: baccd28e7efa4a4a1b2f2c9a1dc57833b27db418 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +04002 * Copyright (c) International Business Machines Corp., 2001
plars865695b2001-08-27 22:15:12 +00003 *
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +04004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
plars865695b2001-08-27 22:15:12 +00008 *
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +04009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
plars865695b2001-08-27 22:15:12 +000013 *
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +040014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000017 */
18
19/*
20 * Test Name: lchown02
21 *
22 * Test Description:
23 * Verify that,
24 * 1) lchown(2) returns -1 and sets errno to EPERM if the effective user id
25 * of process does not match the owner of the file and the process is
26 * not super user.
27 * 2) lchown(2) returns -1 and sets errno to EACCES if search permission is
28 * denied on a component of the path prefix.
29 * 3) lchown(2) returns -1 and sets errno to EFAULT if pathname points
30 * outside user's accessible address space.
31 * 4) lchown(2) returns -1 and sets errno to ENAMETOOLONG if the pathname
32 * component is too long.
33 * 5) lchown(2) returns -1 and sets errno to ENOTDIR if the directory
34 * component in pathname is not a directory.
35 * 6) lchown(2) returns -1 and sets errno to ENOENT if the specified file
36 * does not exists.
37 *
38 * Expected Result:
39 * lchown() should fail with return value -1 and set expected errno.
40 *
plars865695b2001-08-27 22:15:12 +000041 * HISTORY
42 * 07/2001 Ported by Wayne Boyer
Garrett Coopera99df352010-12-10 09:02:53 -080043 * 11/2010 Rewritten by Cyril Hrubis chrubis@suse.cz
plars865695b2001-08-27 22:15:12 +000044 */
45
46#include <stdio.h>
47#include <stdlib.h>
48#include <unistd.h>
49#include <fcntl.h>
50#include <errno.h>
51#include <string.h>
52#include <signal.h>
53#include <grp.h>
54#include <pwd.h>
55#include <sys/types.h>
56#include <sys/stat.h>
plars1ad84512002-07-23 13:11:18 +000057#include <sys/mman.h>
plars865695b2001-08-27 22:15:12 +000058
59#include "test.h"
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +040060#include "compat_16.h"
plars865695b2001-08-27 22:15:12 +000061
Garrett Coopera99df352010-12-10 09:02:53 -080062#define TEST_USER "nobody"
plars865695b2001-08-27 22:15:12 +000063#define MODE_RWX S_IRWXU | S_IRWXG | S_IRWXO
64#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
65#define DIR_TEMP "testdir_1"
66#define TEST_FILE1 "tfile_1"
67#define SFILE1 "sfile_1"
68#define TEST_FILE2 "testdir_1/tfile_2"
69#define SFILE2 "testdir_1/sfile_2"
Garrett Coopera99df352010-12-10 09:02:53 -080070#define TFILE3 "t_file"
71#define SFILE3 "t_file/sfile"
plars865695b2001-08-27 22:15:12 +000072
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +040073TCID_DEFINE(lchown02);
Garrett Coopera99df352010-12-10 09:02:53 -080074int TST_TOTAL = 7;
plars865695b2001-08-27 22:15:12 +000075
Garrett Coopera99df352010-12-10 09:02:53 -080076static void setup_eperm(int pos);
77static void setup_eacces(int pos);
78static void setup_enotdir(int pos);
79static void setup_longpath(int pos);
80static void setup_efault(int pos);
81static void setup_highaddress(int pos);
plars865695b2001-08-27 22:15:12 +000082
Garrett Coopera99df352010-12-10 09:02:53 -080083static char path[PATH_MAX + 2];
84
85struct test_case_t {
plars865695b2001-08-27 22:15:12 +000086 char *pathname;
87 char *desc;
88 int exp_errno;
Wanlong Gao354ebb42012-12-07 10:10:04 +080089 void (*setup) (int pos);
plars865695b2001-08-27 22:15:12 +000090};
91
Garrett Coopera99df352010-12-10 09:02:53 -080092static struct test_case_t test_cases[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080093 {SFILE1, "Process is not owner/root", EPERM, setup_eperm},
94 {SFILE2, "Search permission denied", EACCES, setup_eacces},
95 {NULL, "Address beyond address space", EFAULT, setup_highaddress},
96 {NULL, "Unaccessible address space", EFAULT, setup_efault},
97 {path, "Pathname too long", ENAMETOOLONG, setup_longpath},
98 {SFILE3, "Path contains regular file", ENOTDIR, setup_enotdir},
99 {"", "Pathname is empty", ENOENT, NULL},
100 {NULL, NULL, 0, NULL}
Garrett Coopera99df352010-12-10 09:02:53 -0800101};
plars865695b2001-08-27 22:15:12 +0000102
Garrett Coopera99df352010-12-10 09:02:53 -0800103static struct passwd *ltpuser;
robbiew5f2ac9f2001-08-31 18:29:29 +0000104
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +0400105static void setup(void);
106static void cleanup(void);
plars865695b2001-08-27 22:15:12 +0000107
Garrett Coopera99df352010-12-10 09:02:53 -0800108int main(int argc, char *argv[])
plars865695b2001-08-27 22:15:12 +0000109{
Garrett Coopera99df352010-12-10 09:02:53 -0800110 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200111 const char *msg;
Garrett Coopera99df352010-12-10 09:02:53 -0800112 uid_t user_id;
113 gid_t group_id;
114 int i;
subrata_modakbdbaec52009-02-26 12:14:51 +0000115
Garrett Cooper5ded27d2010-12-19 07:35:08 -0800116 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +0000117 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000118
plars865695b2001-08-27 22:15:12 +0000119 setup();
plars865695b2001-08-27 22:15:12 +0000120
Garrett Coopera99df352010-12-10 09:02:53 -0800121 user_id = geteuid();
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +0400122 UID16_CHECK(user_id, lchown, cleanup);
Garrett Coopera99df352010-12-10 09:02:53 -0800123 group_id = getegid();
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +0400124 GID16_CHECK(group_id, lchown, cleanup);
plars865695b2001-08-27 22:15:12 +0000125
plars865695b2001-08-27 22:15:12 +0000126 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800127 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000128
Garrett Coopera99df352010-12-10 09:02:53 -0800129 for (i = 0; test_cases[i].desc != NULL; i++) {
130 char *file_name = test_cases[i].pathname;
131 char *test_desc = test_cases[i].desc;
plars865695b2001-08-27 22:15:12 +0000132
133 /*
134 * Call lchown(2) to test different test conditions.
135 * verify that it fails with -1 return value and
136 * sets appropriate errno.
137 */
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +0400138 TEST(LCHOWN(cleanup, file_name, user_id, group_id));
subrata_modakbdbaec52009-02-26 12:14:51 +0000139
plars865695b2001-08-27 22:15:12 +0000140 /* Check return code from lchown(2) */
141 if (TEST_RETURN == -1) {
Garrett Coopera99df352010-12-10 09:02:53 -0800142 if (TEST_ERRNO == test_cases[i].exp_errno) {
plars865695b2001-08-27 22:15:12 +0000143 tst_resm(TPASS,
Garrett Coopera99df352010-12-10 09:02:53 -0800144 "lchown(2) fails, %s, errno:%d",
plars865695b2001-08-27 22:15:12 +0000145 test_desc, TEST_ERRNO);
146 } else {
Garrett Coopera99df352010-12-10 09:02:53 -0800147 tst_resm(TFAIL, "lchown(2) fails, %s, "
plars865695b2001-08-27 22:15:12 +0000148 "errno:%d, expected errno:%d",
149 test_desc, TEST_ERRNO,
Garrett Coopera99df352010-12-10 09:02:53 -0800150 test_cases[i].exp_errno);
plars865695b2001-08-27 22:15:12 +0000151 }
152 } else {
Garrett Coopera99df352010-12-10 09:02:53 -0800153 tst_resm(TFAIL, "lchown(2) returned %ld, "
plars865695b2001-08-27 22:15:12 +0000154 "expected -1, errno:%d", TEST_RETURN,
Garrett Coopera99df352010-12-10 09:02:53 -0800155 test_cases[i].exp_errno);
plars865695b2001-08-27 22:15:12 +0000156 }
Garrett Coopera99df352010-12-10 09:02:53 -0800157 }
158 }
plars865695b2001-08-27 22:15:12 +0000159
plars865695b2001-08-27 22:15:12 +0000160 cleanup();
Garrett Cooper5ded27d2010-12-19 07:35:08 -0800161 tst_exit();
Garrett Coopera99df352010-12-10 09:02:53 -0800162}
plars865695b2001-08-27 22:15:12 +0000163
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +0400164static void setup(void)
plars865695b2001-08-27 22:15:12 +0000165{
Garrett Coopera99df352010-12-10 09:02:53 -0800166 int i;
yaberauneya3dfbae92009-11-14 07:14:06 +0000167
plars865695b2001-08-27 22:15:12 +0000168 tst_sig(FORK, DEF_HANDLER, cleanup);
169
Garrett Cooper5ded27d2010-12-19 07:35:08 -0800170 tst_require_root(NULL);
Garrett Coopera99df352010-12-10 09:02:53 -0800171
172 TEST_PAUSE;
Garrett Cooper2c282152010-12-16 00:55:50 -0800173
Garrett Coopera99df352010-12-10 09:02:53 -0800174 /* change euid and gid to nobody */
175 ltpuser = getpwnam(TEST_USER);
176
Garrett Cooper5ded27d2010-12-19 07:35:08 -0800177 if (ltpuser == NULL)
178 tst_brkm(TBROK, cleanup, "getpwnam failed");
robbiew691a9c82001-09-12 16:30:29 +0000179
Garrett Cooper5ded27d2010-12-19 07:35:08 -0800180 if (setgid(ltpuser->pw_uid) == -1)
181 tst_resm(TBROK | TERRNO, "setgid failed");
Garrett Cooper2c282152010-12-16 00:55:50 -0800182
plars865695b2001-08-27 22:15:12 +0000183 tst_tmpdir();
184
Garrett Cooper5ded27d2010-12-19 07:35:08 -0800185 for (i = 0; test_cases[i].desc != NULL; i++)
Garrett Coopera99df352010-12-10 09:02:53 -0800186 if (test_cases[i].setup != NULL)
187 test_cases[i].setup(i);
plars865695b2001-08-27 22:15:12 +0000188}
189
190/*
Garrett Coopera99df352010-12-10 09:02:53 -0800191 * setup_eperm() - setup function for a test condition for which lchown(2)
192 * returns -1 and sets errno to EPERM.
subrata_modak4bb656a2009-02-26 12:02:09 +0000193 *
Garrett Coopera99df352010-12-10 09:02:53 -0800194 * Create test file and symlink with uid 0.
plars865695b2001-08-27 22:15:12 +0000195 */
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +0400196static void setup_eperm(int pos LTP_ATTRIBUTE_UNUSED)
plars865695b2001-08-27 22:15:12 +0000197{
Garrett Coopera99df352010-12-10 09:02:53 -0800198 int fd;
plars865695b2001-08-27 22:15:12 +0000199
Garrett Coopera99df352010-12-10 09:02:53 -0800200 /* create a testfile */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800201 if ((fd = open(TEST_FILE1, O_RDWR | O_CREAT, 0666)) == -1)
202 tst_brkm(TBROK | TERRNO, cleanup, "open failed");
Garrett Coopera99df352010-12-10 09:02:53 -0800203
Garrett Cooper5ded27d2010-12-19 07:35:08 -0800204 if (close(fd) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800205 tst_brkm(TBROK | TERRNO, cleanup, "close failed");
Garrett Cooper2c282152010-12-16 00:55:50 -0800206
Garrett Coopera99df352010-12-10 09:02:53 -0800207 /* become root once more */
Garrett Cooper5ded27d2010-12-19 07:35:08 -0800208 if (seteuid(0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800209 tst_resm(TBROK | TERRNO, "setuid(0) failed");
plars865695b2001-08-27 22:15:12 +0000210
Garrett Coopera99df352010-12-10 09:02:53 -0800211 /* create symling to testfile */
Garrett Cooper5ded27d2010-12-19 07:35:08 -0800212 if (symlink(TEST_FILE1, SFILE1) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800213 tst_brkm(TBROK | TERRNO, cleanup, "symlink failed");
plars865695b2001-08-27 22:15:12 +0000214
Garrett Coopera99df352010-12-10 09:02:53 -0800215 /* back to the user nobody */
Garrett Cooper5ded27d2010-12-19 07:35:08 -0800216 if (seteuid(ltpuser->pw_uid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800217 tst_resm(TBROK | TERRNO, "seteuid(%d) failed", ltpuser->pw_uid);
plars865695b2001-08-27 22:15:12 +0000218}
219
220/*
Garrett Coopera99df352010-12-10 09:02:53 -0800221 * setup_eaccess() - setup function for a test condition for which lchown(2)
222 * returns -1 and sets errno to EACCES.
223 *
plars865695b2001-08-27 22:15:12 +0000224 * Create a test directory under temporary directory and create a test file
225 * under this directory with mode "0666" permissions.
226 * Modify the mode permissions on test directory such that process will not
227 * have search permissions on test directory.
plars865695b2001-08-27 22:15:12 +0000228 */
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +0400229static void setup_eacces(int pos LTP_ATTRIBUTE_UNUSED)
plars865695b2001-08-27 22:15:12 +0000230{
231 int fd;
subrata_modakbdbaec52009-02-26 12:14:51 +0000232
Garrett Coopera99df352010-12-10 09:02:53 -0800233 /* create a test directory */
Garrett Cooper5ded27d2010-12-19 07:35:08 -0800234 if (mkdir(DIR_TEMP, MODE_RWX) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800235 tst_brkm(TBROK | TERRNO, cleanup, "mkdir failed");
Garrett Coopera99df352010-12-10 09:02:53 -0800236
237 /* create a file under test directory */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800238 if ((fd = open(TEST_FILE2, O_RDWR | O_CREAT, 0666)) == -1)
239 tst_brkm(TBROK | TERRNO, cleanup, "open failed");
Garrett Cooper2c282152010-12-16 00:55:50 -0800240
Garrett Cooper5ded27d2010-12-19 07:35:08 -0800241 if (close(fd) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800242 tst_brkm(TBROK | TERRNO, cleanup, "close failed");
Garrett Coopera99df352010-12-10 09:02:53 -0800243
244 /* create a symlink of testfile */
245 if (symlink(TEST_FILE2, SFILE2) < 0) {
246 tst_brkm(TBROK | TERRNO, cleanup, "symlink(2) %s to %s failed",
247 TEST_FILE2, SFILE2);
248 }
249
250 /* modify mode permissions on test directory */
251 if (chmod(DIR_TEMP, FILE_MODE) < 0) {
252 tst_brkm(TBROK | TERRNO, cleanup, "chmod(2) %s failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800253 DIR_TEMP);
Garrett Coopera99df352010-12-10 09:02:53 -0800254 }
255}
256
257/*
258 * setup_efault() -- setup for a test condition where lchown(2) returns -1 and
259 * sets errno to EFAULT.
260 *
261 * Create "bad address" by explicitly mmaping anonymous page that may not be
262 * accesed (see PROT_NONE).
263 */
264static void setup_efault(int pos)
265{
266 char *bad_addr = 0;
Garrett Cooper2c282152010-12-16 00:55:50 -0800267
Garrett Coopera99df352010-12-10 09:02:53 -0800268 bad_addr = mmap(NULL, 1, PROT_NONE,
269 MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, -1, 0);
270
271 if (bad_addr == MAP_FAILED)
272 tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
273
274 test_cases[pos].pathname = bad_addr;
275}
276
277/*
278 * setup_efault() -- setup for a test condition where lchown(2) returns -1 and
279 * sets errno to EFAULT.
280 *
281 * Use ltp function get_high_address() to compute high address.
282 */
283static void setup_highaddress(int pos)
284{
285 test_cases[pos].pathname = get_high_address();
286}
287
288/*
289 * setup_enotdir() - setup function for a test condition for which chown(2)
290 * returns -1 and sets errno to ENOTDIR.
291 *
292 * Create a regular file "t_file" to call lchown(2) on "t_file/sfile" later.
293 */
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +0400294static void setup_enotdir(int pos LTP_ATTRIBUTE_UNUSED)
Garrett Coopera99df352010-12-10 09:02:53 -0800295{
296 int fd;
297
298 /* create a testfile under temporary directory */
299 if ((fd = open(TFILE3, O_RDWR | O_CREAT, MODE_RWX)) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800300 tst_brkm(TBROK | TERRNO, cleanup, "open(2) %s failed", TFILE3);
Garrett Coopera99df352010-12-10 09:02:53 -0800301 }
302
303 if (close(fd) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800304 tst_brkm(TBROK | TERRNO, cleanup, "close(2) %s failed", TFILE3);
Garrett Coopera99df352010-12-10 09:02:53 -0800305 }
plars865695b2001-08-27 22:15:12 +0000306}
307
308/*
309 * longpath_setup() - setup to create a node with a name length exceeding
Garrett Coopera99df352010-12-10 09:02:53 -0800310 * the length of PATH_MAX.
plars865695b2001-08-27 22:15:12 +0000311 */
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +0400312static void setup_longpath(int pos)
plars865695b2001-08-27 22:15:12 +0000313{
Garrett Coopera99df352010-12-10 09:02:53 -0800314 memset(test_cases[pos].pathname, 'a', PATH_MAX + 1);
315 test_cases[pos].pathname[PATH_MAX + 1] = '\0';
plars865695b2001-08-27 22:15:12 +0000316}
317
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +0400318static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000319{
Garrett Coopera99df352010-12-10 09:02:53 -0800320 if (seteuid(0) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800321 tst_resm(TINFO | TERRNO,
322 "seteuid(2) failed to set the effective uid to 0");
plars865695b2001-08-27 22:15:12 +0000323 }
324
plars865695b2001-08-27 22:15:12 +0000325 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700326}