blob: 510be37e4eefdc50b90ab4f337061ff6370113b9 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +02002 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 Ported by Wayne Boyer
4 * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz>
plars865695b2001-08-27 22:15:12 +00005 *
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +02006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
plars865695b2001-08-27 22:15:12 +000010 *
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +020011 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
plars865695b2001-08-27 22:15:12 +000015 *
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +020016 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000019 */
20
vapier37cf54a2007-04-13 20:58:17 +000021/*
plars865695b2001-08-27 22:15:12 +000022 * Test Description:
23 * Verify that, when fchown(2) invoked by super-user to change the owner and
vapier37cf54a2007-04-13 20:58:17 +000024 * group of a file specified by file descriptor to any numeric
plars865695b2001-08-27 22:15:12 +000025 * owner(uid)/group(gid) values,
26 * - clears setuid and setgid bits set on an executable file.
27 * - preserves setgid bit set on a non-group-executable file.
plars865695b2001-08-27 22:15:12 +000028 */
29
30#include <stdio.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <sys/fcntl.h>
34#include <errno.h>
35#include <string.h>
36#include <signal.h>
37
38#include "test.h"
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +020039#include "safe_macros.h"
Han Pingtian75fb0572014-11-28 16:33:41 +080040#include "compat_16.h"
plars865695b2001-08-27 22:15:12 +000041
42#define FILE_MODE S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
43#define NEW_PERMS1 S_IFREG | S_IRWXU | S_IRWXG | S_ISUID | S_ISGID
44#define NEW_PERMS2 S_IFREG | S_IRWXU | S_ISGID
45#define EXP_PERMS S_IFREG | S_IRWXU | S_IRWXG
46#define TESTFILE1 "testfile1"
47#define TESTFILE2 "testfile2"
48
Han Pingtian75fb0572014-11-28 16:33:41 +080049TCID_DEFINE(fchown02);
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +020050int TST_TOTAL = 2;
51static int fd1;
52static int fd2;
plars865695b2001-08-27 22:15:12 +000053
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +020054struct test_case {
55 int *fd;
plars865695b2001-08-27 22:15:12 +000056 char *pathname;
57 char *desc;
58 uid_t user_id;
59 gid_t group_id;
60 int test_flag;
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +020061} tc[] = {
62 {&fd1, TESTFILE1, "Setuid/Setgid bits cleared", 700, 701, 1},
63 {&fd2, TESTFILE2, "Setgid bit not cleared", 700, 701, 2},
64 {0, NULL, NULL, 0, 0, 0}
plars865695b2001-08-27 22:15:12 +000065};
66
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +020067static void setup(void);
68static void cleanup(void);
69
70static void verify_fchown(struct test_case *t)
71{
72 struct stat stat_buf;
73
Han Pingtian75fb0572014-11-28 16:33:41 +080074 TEST(FCHOWN(cleanup, *t->fd, t->user_id, t->group_id));
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +020075
76 if (TEST_RETURN == -1) {
77 tst_resm(TFAIL | TTERRNO, "fchown() Fails on %s", t->pathname);
78 return;
79 }
80
81 SAFE_FSTAT(cleanup, *t->fd, &stat_buf);
82
83 if ((stat_buf.st_uid != t->user_id) ||
84 (stat_buf.st_gid != t->group_id)) {
85 tst_resm(TFAIL,
86 "%s: Incorrect ownership expected %d %d, have %d %d",
87 t->pathname, t->user_id, t->group_id,
88 stat_buf.st_uid, stat_buf.st_gid);
89 }
90
91 switch (t->test_flag) {
92 case 1:
93 if (((stat_buf.st_mode & (S_ISUID | S_ISGID)))) {
94 tst_resm(TFAIL, "%s: Incorrect mode "
95 "permissions %#o, Expected "
96 "%#o", t->pathname, NEW_PERMS1,
97 EXP_PERMS);
98 return;
99 }
100 break;
101 case 2:
102 if ((!(stat_buf.st_mode & S_ISGID))) {
103 tst_resm(TFAIL,
104 "%s: Incorrect mode "
105 "permissions %#o, Expected "
106 "%#o", t->pathname,
107 stat_buf.st_mode, NEW_PERMS2);
108 return;
109 }
110 break;
111 }
112
113 tst_resm(TPASS, "fchown() on %s succeeds : %s", t->pathname, t->desc);
114}
plars865695b2001-08-27 22:15:12 +0000115
vapier37cf54a2007-04-13 20:58:17 +0000116int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000117{
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +0200118 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200119 const char *msg;
vapier37cf54a2007-04-13 20:58:17 +0000120
Garrett Cooper45e285d2010-11-22 12:19:25 -0800121 msg = parse_opts(ac, av, NULL, NULL);
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +0200122 if (msg != NULL)
plars865695b2001-08-27 22:15:12 +0000123 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800124
plars865695b2001-08-27 22:15:12 +0000125 setup();
126
plars865695b2001-08-27 22:15:12 +0000127 for (lc = 0; TEST_LOOPING(lc); lc++) {
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +0200128 for (i = 0; tc[i].desc != NULL; i++)
129 verify_fchown(tc + i);
Garrett Cooper2c282152010-12-16 00:55:50 -0800130 }
plars865695b2001-08-27 22:15:12 +0000131
plars865695b2001-08-27 22:15:12 +0000132 cleanup();
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +0200133 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800134}
plars865695b2001-08-27 22:15:12 +0000135
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +0200136static void setup(void)
plars865695b2001-08-27 22:15:12 +0000137{
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +0200138 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000139
plars865695b2001-08-27 22:15:12 +0000140 tst_sig(NOFORK, DEF_HANDLER, cleanup);
141
plars865695b2001-08-27 22:15:12 +0000142 TEST_PAUSE;
vapier37cf54a2007-04-13 20:58:17 +0000143
plars865695b2001-08-27 22:15:12 +0000144 tst_tmpdir();
145
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +0200146 fd1 = SAFE_OPEN(cleanup, TESTFILE1, O_RDWR | O_CREAT, FILE_MODE);
147 SAFE_CHMOD(cleanup, TESTFILE1, NEW_PERMS1);
148 fd2 = SAFE_OPEN(cleanup, TESTFILE2, O_RDWR | O_CREAT, FILE_MODE);
149 SAFE_CHMOD(cleanup, TESTFILE2, NEW_PERMS2);
Garrett Cooper2c282152010-12-16 00:55:50 -0800150}
plars865695b2001-08-27 22:15:12 +0000151
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +0200152static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000153{
Cyril Hrubisc5f6cad2014-06-03 14:54:05 +0200154 if (fd1 > 0 && close(fd1))
155 tst_resm(TWARN | TERRNO, "Failed to close fd1");
156
157 if (fd2 > 0 && close(fd2))
158 tst_resm(TWARN | TERRNO, "Failed to close fd2");
plars865695b2001-08-27 22:15:12 +0000159
plars865695b2001-08-27 22:15:12 +0000160 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700161}