plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * |
| 4 | * 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. |
| 8 | * |
| 9 | * 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. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 19 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 20 | * Test Name: chmod03 |
| 21 | * |
| 22 | * Test Description: |
| 23 | * Verify that, chmod(2) will succeed to change the mode of a file |
| 24 | * and set the sticky bit on it if invoked by non-root (uid != 0) |
| 25 | * process with the following constraints, |
| 26 | * - the process is the owner of the file. |
| 27 | * - the effective group ID or one of the supplementary group ID's of the |
| 28 | * process is equal to the group ID of the file. |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 29 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | * Expected Result: |
| 31 | * chmod() should return value 0 on success and succeeds to change |
| 32 | * the mode of specified file with sticky bit set on it. |
| 33 | * |
| 34 | * Algorithm: |
| 35 | * Setup: |
| 36 | * Setup signal handling. |
| 37 | * Create temporary directory. |
| 38 | * Pause for SIGUSR1 if option specified. |
| 39 | * |
| 40 | * Test: |
| 41 | * Loop if the proper options are given. |
| 42 | * Execute system call |
| 43 | * Check return code, if system call failed (return=-1) |
| 44 | * Log the errno and Issue a FAIL message. |
| 45 | * Otherwise, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 46 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | * if successful, |
| 48 | * Issue Functionality-Pass message. |
| 49 | * Otherwise, |
| 50 | * Issue Functionality-Fail message. |
| 51 | * Cleanup: |
| 52 | * Print errno log and/or timing stats if options given |
| 53 | * Delete the temporary directory created. |
| 54 | * |
| 55 | * Usage: <for command-line> |
| 56 | * chmod03 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t] |
| 57 | * where, -c n : Run n copies concurrently. |
| 58 | * -e : Turn on errno logging. |
| 59 | * -f : Turn off functionality Testing. |
| 60 | * -i n : Execute test n times. |
| 61 | * -I x : Execute test for x seconds. |
| 62 | * -P x : Pause for x seconds between iterations. |
| 63 | * -t : Turn on syscall timing. |
| 64 | * |
| 65 | * HISTORY |
| 66 | * 07/2001 Ported by Wayne Boyer |
| 67 | * |
| 68 | * RESTRICTIONS: |
| 69 | * This test should be run by 'non-super-user' only. |
| 70 | * |
| 71 | */ |
| 72 | |
| 73 | #include <stdio.h> |
| 74 | #include <sys/types.h> |
| 75 | #include <sys/stat.h> |
| 76 | #include <sys/fcntl.h> |
| 77 | #include <errno.h> |
| 78 | #include <string.h> |
| 79 | #include <signal.h> |
robbiew | a7142d9 | 2001-08-28 18:21:45 +0000 | [diff] [blame] | 80 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
| 82 | #include "test.h" |
| 83 | #include "usctest.h" |
| 84 | |
| 85 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 86 | #define PERMS 01777 /* |
| 87 | * Mode permissions of test file with sticky |
| 88 | * bit set. |
| 89 | */ |
| 90 | #define TESTFILE "testfile" |
| 91 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 92 | char *TCID = "chmod03"; /* Test program identifier. */ |
| 93 | int TST_TOTAL = 1; /* Total number of test cases. */ |
robbiew | a7142d9 | 2001-08-28 18:21:45 +0000 | [diff] [blame] | 94 | char nobody_uid[] = "nobody"; |
| 95 | struct passwd *ltpuser; |
| 96 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | void setup(); /* Main setup function for the test */ |
| 98 | void cleanup(); /* Main cleanup function for the test */ |
| 99 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 100 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | { |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 102 | struct stat stat_buf; |
| 103 | int lc; |
| 104 | char *msg; |
| 105 | mode_t file_mode; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 107 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 109 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | setup(); |
| 111 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 113 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 114 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | TEST(chmod(TESTFILE, PERMS)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 117 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 119 | tst_resm(TFAIL | TTERRNO, "chmod(%s, %#o) failed", |
vapier | 4599609 | 2009-08-28 11:49:07 +0000 | [diff] [blame] | 120 | TESTFILE, PERMS); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | continue; |
| 122 | } |
| 123 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | if (STD_FUNCTIONAL_TEST) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | if (stat(TESTFILE, &stat_buf) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 126 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 127 | "stat(%s) failed", TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | } |
| 129 | file_mode = stat_buf.st_mode; |
| 130 | |
| 131 | /* Verify STICKY BIT set on testfile */ |
| 132 | if ((file_mode & PERMS) != PERMS) { |
| 133 | tst_resm(TFAIL, "%s: Incorrect modes 0%3o, " |
| 134 | "Expected 0777", TESTFILE, file_mode); |
| 135 | } else { |
| 136 | tst_resm(TPASS, "Functionality of " |
| 137 | "chmod(%s, %#o) successful", |
| 138 | TESTFILE, PERMS); |
| 139 | } |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 140 | } else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | tst_resm(TPASS, "call succeeded"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 142 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | cleanup(); |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 145 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 147 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 149 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | { |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 151 | int fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 154 | |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 155 | tst_require_root(NULL); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 156 | ltpuser = getpwnam(nobody_uid); |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 157 | if (ltpuser == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 158 | tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed"); |
vapier | 4599609 | 2009-08-28 11:49:07 +0000 | [diff] [blame] | 159 | if (setuid(ltpuser->pw_uid) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 160 | tst_brkm(TBROK | TERRNO, NULL, "setuid(%u) failed", |
| 161 | ltpuser->pw_uid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 163 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | tst_tmpdir(); |
| 166 | |
| 167 | /* |
| 168 | * Create a test file under temporary directory with specified |
| 169 | * mode permissios and set the ownership of the test file to the |
| 170 | * uid/gid of guest user. |
| 171 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 172 | if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
| 173 | tst_brkm(TBROK | TERRNO, cleanup, |
vapier | 4599609 | 2009-08-28 11:49:07 +0000 | [diff] [blame] | 174 | "open(%s, O_RDWR|O_CREAT, %#o) failed", |
| 175 | TESTFILE, FILE_MODE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | } |
| 177 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 178 | if (close(fd) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 179 | tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed", TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 181 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | |
| 183 | /* |
| 184 | * void |
| 185 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 186 | * completion or premature exit. |
| 187 | * Delete the testfile and temporary directory created in setup(). |
| 188 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 189 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | { |
| 191 | /* |
| 192 | * print timing stats if that option was specified. |
| 193 | */ |
| 194 | TEST_CLEANUP; |
| 195 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | tst_rmdir(); |
| 197 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 198 | } |