From: <mic...@pe...> - 2009-04-24 06:40:24
|
From: Michal Simek <mo...@mo...> This patch is based on Jiri's change_owner patch. I removed references in runtest/syscalls and fix some coding style violations. Signed-off-by: Michal Simek <mo...@mo...> --- runtest/syscalls | 24 +++--- testcases/kernel/syscalls/chmod/Makefile | 6 +- testcases/kernel/syscalls/chmod/change_owner.c | 94 -------------------- .../kernel/syscalls/chmod/change_owner.mode.sh | 16 ---- testcases/kernel/syscalls/chmod/chmod05.c | 64 ++++++-------- testcases/kernel/syscalls/chmod/chmod06.c | 38 ++------ testcases/kernel/syscalls/chown/Makefile | 6 +- testcases/kernel/syscalls/chown/change_owner.c | 94 -------------------- .../kernel/syscalls/chown/change_owner.mode.sh | 16 ---- testcases/kernel/syscalls/chown/chown03.c | 91 ++++++------------- testcases/kernel/syscalls/chown/chown04.c | 76 +++++++--------- testcases/kernel/syscalls/fchmod/Makefile | 6 +- testcases/kernel/syscalls/fchmod/change_owner.c | 94 -------------------- .../kernel/syscalls/fchmod/change_owner.mode.sh | 16 ---- testcases/kernel/syscalls/fchmod/fchmod05.c | 59 ++++++------- testcases/kernel/syscalls/fchmod/fchmod06.c | 32 ++----- testcases/kernel/syscalls/fchown/Makefile | 6 +- testcases/kernel/syscalls/fchown/change_owner.c | 94 -------------------- .../kernel/syscalls/fchown/change_owner.mode.sh | 16 ---- testcases/kernel/syscalls/fchown/fchown03.c | 65 ++++--------- testcases/kernel/syscalls/fchown/fchown04.c | 64 +++++-------- 21 files changed, 197 insertions(+), 780 deletions(-) delete mode 100644 testcases/kernel/syscalls/chmod/change_owner.c delete mode 100644 testcases/kernel/syscalls/chmod/change_owner.mode.sh delete mode 100644 testcases/kernel/syscalls/chown/change_owner.c delete mode 100644 testcases/kernel/syscalls/chown/change_owner.mode.sh delete mode 100644 testcases/kernel/syscalls/fchmod/change_owner.c delete mode 100644 testcases/kernel/syscalls/fchmod/change_owner.mode.sh delete mode 100644 testcases/kernel/syscalls/fchown/change_owner.c delete mode 100644 testcases/kernel/syscalls/fchown/change_owner.mode.sh diff --git a/runtest/syscalls b/runtest/syscalls index d580d43..9b7b2ca 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -49,18 +49,18 @@ chmod01A symlink01 -T chmod01 chmod02 chmod02 chmod03 chmod03 chmod04 chmod04 -chmod05 cp -p $LTPROOT/testcases/bin/change_owner $TMP;chmod05 -chmod06 cp -p $LTPROOT/testcases/bin/change_owner $TMP;chmod06 +chmod05 chmod05 +chmod06 chmod06 chmod07 chmod07 chown01 chown01 chown01_16 chown01_16 chown02 chown02 chown02_16 chown02_16 -chown03 export change_owner=$LTPROOT/testcases/bin/change_owner;chown03 -chown03_16 export change_owner=$LTPROOT/testcases/bin/change_owner;chown03_16 -chown04 cp -p $LTPROOT/testcases/bin/change_owner $TMP;chown04 -chown04_16 cp -p $LTPROOT/testcases/bin/change_owner $TMP;chown04_16 +chown03 chown03 +chown03_16 chown03_16 +chown04 chown04 +chown04_16 chown04_16 chown05 chown05 chown05_16 chown05_16 @@ -162,8 +162,8 @@ fchmod01 fchmod01 fchmod02 fchmod02 fchmod03 fchmod03 fchmod04 fchmod04 -fchmod05 cp -p $LTPROOT/testcases/bin/change_owner $TMP;fchmod05 -fchmod06 cp -p $LTPROOT/testcases/bin/change_owner $TMP;fchmod06 +fchmod05 fchmod05 +fchmod06 fchmod06 fchmod07 fchmod07 #fchmodat test cases @@ -173,10 +173,10 @@ fchown01 fchown01 fchown01_16 fchown01_16 fchown02 fchown02 fchown02_16 fchown02_16 -fchown03 cp -p $LTPROOT/testcases/bin/change_owner $TMP;fchown03 -fchown03_16 cp -p $LTPROOT/testcases/bin/change_owner $TMP;fchown03_16 -fchown04 export change_owner=$LTPROOT/testcases/bin/change_owner;fchown04 -fchown04_16 export change_owner=$LTPROOT/testcases/bin/change_owner;fchown04_16 +fchown03 fchown03 +fchown03_16 fchown03_16 +fchown04 fchown04 +fchown04_16 fchown04_16 fchown05 fchown05 fchown05_16 fchown05_16 diff --git a/testcases/kernel/syscalls/chmod/Makefile b/testcases/kernel/syscalls/chmod/Makefile index dfa3b86..c3f60c5 100644 --- a/testcases/kernel/syscalls/chmod/Makefile +++ b/testcases/kernel/syscalls/chmod/Makefile @@ -24,13 +24,9 @@ TARGETS = $(patsubst %.c,%,$(SRCS)) all: $(TARGETS) -install: change_owner.mode +install: @set -e; for i in $(TARGETS); do ln -f $$i ../../../bin/$$i ; done clean: rm -f $(TARGETS) -change_owner.mode: - @/bin/sh ./change_owner.mode.sh - -.PHONY: change_owner.mode diff --git a/testcases/kernel/syscalls/chmod/change_owner.c b/testcases/kernel/syscalls/chmod/change_owner.c deleted file mode 100644 index 09aeb22..0000000 --- a/testcases/kernel/syscalls/chmod/change_owner.c +++ /dev/null @@ -1,94 +0,0 @@ -/* - * - * Copyright (c) International Business Machines Corp., 2001 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See - * the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Description: This is a setuid to root program invoked by a non-root - * process to change the user id/group id bits on the test - * directory/file created in the setup function. - * - * This function exit with 0 or 1 depending upon the - * success/failure of chown(2) system call. - */ -#include <stdio.h> -#include <unistd.h> -#include <stdlib.h> -#include <string.h> -#include <sys/types.h> -#include <pwd.h> -#include <grp.h> -#include <errno.h> - -int main(int argc, char **argv) -{ - struct passwd *ltpuser; /* password struct for ltpuser2 */ - struct group *ltpgroup; /* group struct for ltpuser2 */ - uid_t user_uid; /* user id of ltpuser2 */ - gid_t group_gid; /* group id of ltpuser2 */ - char *test_name; /* test specific name */ - char *path_name; /* name of test directory/file */ - - if (argc != 3) { - fprintf(stderr, - "This is a helper binary meant for internal LTP usage only\n"); - exit(1); - } - - test_name = argv[1]; - path_name = argv[2]; - - /* - * Get the user id and group id of "ltpuser2" user from password - * and group files. - */ - if ((ltpuser = getpwnam("nobody")) == NULL) { - perror("change_owner: nobody not found in /etc/passwd"); - exit(1); - } - if ((ltpgroup = getgrnam("nobody")) == NULL) { - if ((ltpgroup = getgrnam("nogroup")) == NULL) { - perror - ("change_owner: nobody/nogroup's group not found in /etc/group"); - exit(1); - } - } - - user_uid = 0; - group_gid = 0; - - /* Check for test specific name and set uid/gid accordingly */ - if (!(strcmp(test_name, "fchown03"))) { - user_uid = -1; - group_gid = ltpgroup->gr_gid; - } else if (!(strcmp(test_name, "fchown04"))) { - user_uid = ltpuser->pw_uid; - group_gid = ltpgroup->gr_gid; - } - - /* - * Change the ownership of test directory/file specified by - * pathname to that of user_uid and group_gid. - */ - if (chown(path_name, user_uid, group_gid) < 0) { - fprintf(stderr, "change_owner: chown() of %s failed, error " - "%d\n", path_name, errno); - exit(1); - } - - exit(0); -} diff --git a/testcases/kernel/syscalls/chmod/change_owner.mode.sh b/testcases/kernel/syscalls/chmod/change_owner.mode.sh deleted file mode 100644 index 05f1240..0000000 --- a/testcases/kernel/syscalls/chmod/change_owner.mode.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/sh - -failed() { - echo "" - echo " ************** WARNING **************" - echo " Cannot change permission or ownership of \"change_owner\"." - echo " Tests in this directory will fail"$ - echo " Run "make install" as root." - echo " *************************************" - sleep 2 -} - -chown root change_owner || failed -chmod 04755 change_owner || failed - -exit 0 diff --git a/testcases/kernel/syscalls/chmod/chmod05.c b/testcases/kernel/syscalls/chmod/chmod05.c index d1abc8e..4504aaa 100644 --- a/testcases/kernel/syscalls/chmod/chmod05.c +++ b/testcases/kernel/syscalls/chmod/chmod05.c @@ -46,11 +46,11 @@ * Loop if the proper options are given. * Execute system call * Check return code, if system call failed (return=-1) - * Log the errno and Issue a FAIL message. + * Log the errno and Issue a FAIL message. * Otherwise, - * Verify the Functionality of system call + * Verify the Functionality of system call * if successful, - * Issue Functionality-Pass message. + * Issue Functionality-Pass message. * Otherwise, * Issue Functionality-Fail message. * Cleanup: @@ -105,7 +105,7 @@ #define DEBUG 0 -#define MODE_RWX (mode_t)(S_IRWXU | S_IRWXG | S_IRWXO) +#define MODE_RWX (mode_t)(S_IRWXU | S_IRWXG | S_IRWXO) #define DIR_MODE (mode_t)(S_ISVTX | S_ISGID | S_IFDIR) #define PERMS (mode_t)(MODE_RWX | DIR_MODE) #define TESTDIR "testdir" @@ -174,7 +174,7 @@ int main(int ac, char **av) printf("PERMS = 0%03o\n", PERMS); printf("dir_mode = 0%03o\n", dir_mode); #endif - if (PERMS != dir_mode) { + if ((PERMS & ~S_ISGID) != dir_mode) { tst_resm(TFAIL, "%s: Incorrect modes 0%03o, " "Expected 0%03o", TESTDIR, dir_mode, PERMS); @@ -205,12 +205,8 @@ int main(int ac, char **av) */ void setup() { - char *test_home; /* variable to hold TESTHOME env */ - char Path_name[PATH_MAX]; /* Buffer to hold command string */ - char Cmd_buffer[BUFSIZ]; /* Buffer to hold command string */ - - /* capture signals */ - tst_sig(FORK, DEF_HANDLER, cleanup); + struct passwd *nobody_u; + struct group *bin_group; //wjh Improper comment! This makes sure we _are_ "root" not "nobody" /* Switch to nobody user for correct error code collection */ @@ -218,14 +214,22 @@ void setup() tst_brkm(TBROK, tst_exit, "Test must be run as root"); } - test_home = get_current_dir_name(); - /* Pause if that option was specified */ TEST_PAUSE; /* make a temp directory and cd to it */ tst_tmpdir(); + nobody_u = getpwnam("nobody"); + if (!nobody_u) + tst_brkm(TBROK, cleanup, "Couldn't find uid of nobody: %s", + strerror(errno)); + + bin_group = getgrnam("bin"); + if (!bin_group) + tst_brkm(TBROK, cleanup, "Couldn't find gid of bin: %s", + strerror(errno)); + /* * Create a test directory under temporary directory with specified * mode permissions and change the gid of test directory to that of @@ -233,33 +237,19 @@ void setup() * guest user2. */ if (mkdir(TESTDIR, MODE_RWX) < 0) { - tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", TESTDIR); - } - - /* Get the current working directory of the process */ - if (getcwd(Path_name, sizeof(Path_name)) == NULL) { - tst_brkm(TBROK, cleanup, - "getcwd(3) fails to get working directory of process"); + tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed: %s", TESTDIR, + strerror(errno)); } - /* - * Get the complete path of TESTDIR created - * under temporary directory - */ - strcat(Path_name, "/" TESTDIR); - - /* Get the command name to be executed as setuid to root */ - strcpy((char *)Cmd_buffer, (const char *)test_home); - strcat((char *)Cmd_buffer, (const char *)"/change_owner "); - strcat((char *)Cmd_buffer, TCID); - strcat((char *)Cmd_buffer, " "); - strcat((char *)Cmd_buffer, Path_name); - - if (system((const char *)Cmd_buffer) != 0) { - tst_brkm(TBROK, cleanup, - "Fail to modify %s group ownership", TESTDIR); - } + if (chown(TESTDIR, nobody_u->pw_uid, bin_group->gr_gid) == -1) + tst_brkm(TBROK, cleanup, "Couldn't change owner of testdir: %s", + strerror(errno)); + /* change to nobody:nobody */ + if (setegid(nobody_u->pw_gid) == -1 || + seteuid(nobody_u->pw_uid) == -1) + tst_brkm(TBROK, cleanup, "Couldn't switch to nobody:nobody: %s", + strerror(errno)); } /* End setup() */ /* diff --git a/testcases/kernel/syscalls/chmod/chmod06.c b/testcases/kernel/syscalls/chmod/chmod06.c index 8352a32..3f20bfe 100644 --- a/testcases/kernel/syscalls/chmod/chmod06.c +++ b/testcases/kernel/syscalls/chmod/chmod06.c @@ -103,11 +103,11 @@ #define TEST_FILE2 "testdir_1/tfile_2" #define TEST_FILE3 "t_file/tfile_3" -int no_setup(); /* dummy setup function */ -int setup1(); /* setup function to test chmod for EPERM */ -int setup2(); /* setup function to test chmod for EACCES */ -int setup3(); /* setup function to test chmod for ENOTDIR */ -int longpath_setup(); /* setup function to test chmod for ENAMETOOLONG */ +int no_setup(); /* dummy setup function */ +int setup1(); /* setup function to test chmod for EPERM */ +int setup2(); /* setup function to test chmod for EACCES */ +int setup3(); /* setup function to test chmod for ENOTDIR */ +int longpath_setup(); /* setup function to test chmod for ENAMETOOLONG */ char *get_high_address(); /* Function from ltp-Lib */ @@ -303,8 +303,6 @@ int no_setup() int setup1() { int fd; - char Path_name[PATH_MAX]; /* Buffer to hold command string */ - char Cmd_buffer[BUFSIZ]; /* Buffer to hold command string */ /* open/creat a test file and close it */ if ((fd = open(TEST_FILE1, O_RDWR | O_CREAT, 0666)) == -1) { @@ -312,33 +310,17 @@ int setup1() "open(%s, O_RDWR|O_CREAT, 0666) failed, errno=%d : %s", TEST_FILE1, errno, strerror(errno)); } + + if (fchown(fd, 0, 0) < 0) + tst_brkm(TBROK, cleanup, "Fail to modify %s ownership(s): %s", + TEST_FILE1, strerror(errno)); + if (close(fd) == -1) { tst_brkm(TBROK, cleanup, "close(%s) Failed, errno=%d : %s", TEST_FILE1, errno, strerror(errno)); } - /* Get the current working directory of the process */ - if (getcwd(Path_name, sizeof(Path_name)) == NULL) { - tst_brkm(TBROK, cleanup, - "getcwd(3) fails to get working directory of process"); - } - - /* Get the path of test file created under temporary directory */ - strcat(Path_name, "/" TEST_FILE1); - - /* Get the command name to be executed as setuid to root */ - strcpy((char *)Cmd_buffer, (const char *)test_home); - strcat((char *)Cmd_buffer, "/change_owner "); - strcat((char *)Cmd_buffer, TCID); - strcat((char *)Cmd_buffer, " "); - strcat((char *)Cmd_buffer, Path_name); - - /* Change the ownership of testfile */ - if (system((const char *)Cmd_buffer) != 0) { - tst_brkm(TBROK, cleanup, - "Fail to modify %s ownership(s)!", TEST_FILE1); - } return 0; } diff --git a/testcases/kernel/syscalls/chown/Makefile b/testcases/kernel/syscalls/chown/Makefile index 0e930e5..1ecc0ae 100644 --- a/testcases/kernel/syscalls/chown/Makefile +++ b/testcases/kernel/syscalls/chown/Makefile @@ -26,13 +26,9 @@ TARGETS += $(patsubst %.c,%,$(SRCS)) all: $(TARGETS) -install: change_owner.mode +install: @set -e; for i in $(TARGETS); do ln -f $$i ../../../bin/$$i ; done clean: rm -f $(TARGETS) -change_owner.mode: - @/bin/sh ./change_owner.mode.sh - -.PHONY: change_owner.mode diff --git a/testcases/kernel/syscalls/chown/change_owner.c b/testcases/kernel/syscalls/chown/change_owner.c deleted file mode 100644 index 09aeb22..0000000 --- a/testcases/kernel/syscalls/chown/change_owner.c +++ /dev/null @@ -1,94 +0,0 @@ -/* - * - * Copyright (c) International Business Machines Corp., 2001 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See - * the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Description: This is a setuid to root program invoked by a non-root - * process to change the user id/group id bits on the test - * directory/file created in the setup function. - * - * This function exit with 0 or 1 depending upon the - * success/failure of chown(2) system call. - */ -#include <stdio.h> -#include <unistd.h> -#include <stdlib.h> -#include <string.h> -#include <sys/types.h> -#include <pwd.h> -#include <grp.h> -#include <errno.h> - -int main(int argc, char **argv) -{ - struct passwd *ltpuser; /* password struct for ltpuser2 */ - struct group *ltpgroup; /* group struct for ltpuser2 */ - uid_t user_uid; /* user id of ltpuser2 */ - gid_t group_gid; /* group id of ltpuser2 */ - char *test_name; /* test specific name */ - char *path_name; /* name of test directory/file */ - - if (argc != 3) { - fprintf(stderr, - "This is a helper binary meant for internal LTP usage only\n"); - exit(1); - } - - test_name = argv[1]; - path_name = argv[2]; - - /* - * Get the user id and group id of "ltpuser2" user from password - * and group files. - */ - if ((ltpuser = getpwnam("nobody")) == NULL) { - perror("change_owner: nobody not found in /etc/passwd"); - exit(1); - } - if ((ltpgroup = getgrnam("nobody")) == NULL) { - if ((ltpgroup = getgrnam("nogroup")) == NULL) { - perror - ("change_owner: nobody/nogroup's group not found in /etc/group"); - exit(1); - } - } - - user_uid = 0; - group_gid = 0; - - /* Check for test specific name and set uid/gid accordingly */ - if (!(strcmp(test_name, "fchown03"))) { - user_uid = -1; - group_gid = ltpgroup->gr_gid; - } else if (!(strcmp(test_name, "fchown04"))) { - user_uid = ltpuser->pw_uid; - group_gid = ltpgroup->gr_gid; - } - - /* - * Change the ownership of test directory/file specified by - * pathname to that of user_uid and group_gid. - */ - if (chown(path_name, user_uid, group_gid) < 0) { - fprintf(stderr, "change_owner: chown() of %s failed, error " - "%d\n", path_name, errno); - exit(1); - } - - exit(0); -} diff --git a/testcases/kernel/syscalls/chown/change_owner.mode.sh b/testcases/kernel/syscalls/chown/change_owner.mode.sh deleted file mode 100644 index 05f1240..0000000 --- a/testcases/kernel/syscalls/chown/change_owner.mode.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/sh - -failed() { - echo "" - echo " ************** WARNING **************" - echo " Cannot change permission or ownership of \"change_owner\"." - echo " Tests in this directory will fail"$ - echo " Run "make install" as root." - echo " *************************************" - sleep 2 -} - -chown root change_owner || failed -chmod 04755 change_owner || failed - -exit 0 diff --git a/testcases/kernel/syscalls/chown/chown03.c b/testcases/kernel/syscalls/chown/chown03.c index 9be0ff1..fde107a 100644 --- a/testcases/kernel/syscalls/chown/chown03.c +++ b/testcases/kernel/syscalls/chown/chown03.c @@ -42,11 +42,11 @@ * Loop if the proper options are given. * Execute system call * Check return code, if system call failed (return=-1) - * Log the errno and Issue a FAIL message. + * Log the errno and Issue a FAIL message. * Otherwise, - * Verify the Functionality of system call + * Verify the Functionality of system call * if successful, - * Issue Functionality-Pass message. + * Issue Functionality-Pass message. * Otherwise, * Issue Functionality-Fail message. * Cleanup: @@ -83,8 +83,8 @@ #include "test.h" #include "usctest.h" -#define FILE_MODE S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH -#define NEW_PERMS S_IFREG | S_IRWXU | S_IRWXG | S_ISUID | S_ISGID +#define FILE_MODE (S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) +#define NEW_PERMS (S_IFREG | S_IRWXU | S_IRWXG | S_ISUID | S_ISGID) #define TESTFILE "testfile" char *TCID = "chown03"; /* Test program identifier. */ @@ -104,8 +104,6 @@ int main(int ac, char **av) uid_t User_id; /* Owner id of the test file. */ gid_t Group_id; /* Group id of the test file. */ - STD_FUNCTIONAL_TEST = 0; - /* Parse standard options given to run the test. */ msg = parse_opts(ac, av, (option_t *) NULL, NULL); if (msg != (char *)NULL) { @@ -130,8 +128,6 @@ int main(int ac, char **av) * group id (numeric values) to set it on testfile. */ - seteuid(0); - setegid(0); TEST(chown(TESTFILE, -1, Group_id)); /* check return code of chown(2) */ @@ -159,8 +155,8 @@ int main(int ac, char **av) */ if ((stat_buf.st_uid != User_id) || (stat_buf.st_gid != Group_id)) { - tst_resm(TFAIL, "%s: Incorrect " - "ownership set to %d %d, Expected %d %d", + tst_resm(TFAIL, "%s: Incorrect ownership" + "set to %d %d, Expected %d %d", TESTFILE, stat_buf.st_uid, stat_buf.st_gid, User_id, Group_id); } @@ -169,10 +165,11 @@ int main(int ac, char **av) * Verify that setuid/setgid bits set on the * testfile in setup() are cleared by chown() */ - if (stat_buf.st_mode & (S_ISUID | S_ISGID)) { + if (stat_buf.st_mode != (NEW_PERMS & ~(S_ISUID | S_ISGID))) { tst_resm(TFAIL, "%s: Incorrect mode permissions" " %#o, Expected %#o", TESTFILE, - stat_buf.st_mode, NEW_PERMS); + stat_buf.st_mode, + NEW_PERMS & ~(S_ISUID | S_ISGID)); } else { tst_resm(TPASS, "chown() on %s succeeds, " "clears setuid/gid bits", TESTFILE); @@ -186,7 +183,8 @@ int main(int ac, char **av) cleanup(); return 0; - /*NOTREACHED*/} /* End main */ +/*NOTREACHED*/ +} /* End main */ /* * void @@ -197,11 +195,7 @@ int main(int ac, char **av) */ void setup() { - char test_home[PATH_MAX]; /* variable to hold TESTHOME env */ - int fd; /* file handler for testfile */ - char Path_name[PATH_MAX]; /* Buffer to hold command string */ - char Cmd_buffer[BUFSIZ]; /* Buffer to hold command string */ - char *change_owner_path; + int fd; /* file handler for testfile */ /* Pause if that option was specified */ TEST_PAUSE; @@ -230,60 +224,33 @@ void setup() perror("seteuid"); } - if (getcwd(test_home, sizeof(test_home)) == NULL) { - tst_brkm(TBROK, cleanup, - "getcwd(3) fails to get working directory of process"); - } - /* Create a test file under temporary directory */ if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) { tst_brkm(TBROK, cleanup, "open(%s, O_RDWR|O_CREAT, %o) Failed, errno=%d : %s", TESTFILE, FILE_MODE, errno, strerror(errno)); } - /* Close the test file created above */ - if (close(fd) == -1) { - tst_brkm(TBROK, cleanup, - "close(%s) Failed, errno=%d : %s", - TESTFILE, errno, strerror(errno)); - } - /* - * Change mode permissions on testfile such that - * setuid/setgid bits are set on the testfile. - */ - if (chmod(TESTFILE, NEW_PERMS) < 0) { - tst_brkm(TBROK, cleanup, - "chmod(2) on %s Failed, errno=%d : %s", - TESTFILE, errno, strerror(errno)); - } + if (seteuid(0) == -1) + tst_brkm(TBROK, cleanup, "Couldn't switch to user root: %s", + strerror(errno)); - /* Get the current working directory of the process */ - if (getcwd(Path_name, sizeof(Path_name)) == NULL) { - tst_brkm(TBROK, cleanup, - "getcwd(3) fails to get working directory of process"); - } - /* Get the path of TESTFILE under temporary directory */ - strcat(Path_name, "/" TESTFILE); + if (fchown(fd, -1, 0) < 0) + tst_brkm(TBROK, cleanup, "Fail to modify Ownership of %s: %s", + TESTFILE, strerror(errno)); - /* Set the environment variable change_owner if not already set */ - setenv("change_owner", strcat(test_home, "/change_owner"), 0); + if (fchmod(fd, NEW_PERMS) < 0) + tst_brkm(TBROK, cleanup, "Fail to modify Mode of %s: %s", + TESTFILE, strerror(errno)); - /* Get the command name to be executed as setuid to root */ - if ((change_owner_path = getenv("change_owner")) == NULL) { - tst_brkm(TBROK, cleanup, - "getenv() failed to get the cmd to be execd as setuid to root"); - } + if (seteuid(ltpuser->pw_uid) == -1) + tst_brkm(TBROK, cleanup, "Couldn't switch to user nobody: %s", + strerror(errno)); - strcpy((char *)Cmd_buffer, (const char *)change_owner_path); - strcat((char *)Cmd_buffer, " "); - strcat((char *)Cmd_buffer, TCID); - strcat((char *)Cmd_buffer, " "); - strcat((char *)Cmd_buffer, Path_name); - - if (system((const char *)Cmd_buffer) != 0) { - tst_brkm(TBROK, cleanup, - "Fail to modify Ownership of %s", TESTFILE); + /* Close the test file created above */ + if (close(fd) == -1) { + tst_brkm(TBROK, cleanup, "close(%s) Failed, errno=%d : %s", + TESTFILE, errno, strerror(errno)); } } /* End setup() */ diff --git a/testcases/kernel/syscalls/chown/chown04.c b/testcases/kernel/syscalls/chown/chown04.c index 0e16144..a58f382 100644 --- a/testcases/kernel/syscalls/chown/chown04.c +++ b/testcases/kernel/syscalls/chown/chown04.c @@ -23,8 +23,8 @@ * Test Description: * Verify that, * 1) chown(2) returns -1 and sets errno to EPERM if the effective user id - * of process does not match the owner of the file and the process is - * not super user. + * of process does not match the owner of the file and the process + * is not super user. * 2) chown(2) returns -1 and sets errno to EACCES if search permission is * denied on a component of the path prefix. * 3) chown(2) returns -1 and sets errno to EFAULT if pathname points @@ -49,12 +49,12 @@ * Loop if the proper options are given. * Execute system call * Check return code, if system call failed (return=-1) - * if errno set == expected errno - * Issue sys call fails with expected return value and errno. - * Otherwise, - * Issue sys call fails with unexpected errno. + * if errno set == expected errno + * Issue sys call fails with expected return value and errno. + * Otherwise, + * Issue sys call fails with unexpected errno. * Otherwise, - * Issue sys call returns unexpected value. + * Issue sys call returns unexpected value. * * Cleanup: * Print errno log and/or timing stats if options given @@ -100,10 +100,10 @@ #define TEST_FILE3 "t_file/tfile_3" int no_setup(); -int setup1(); /* setup function to test chown for EPERM */ -int setup2(); /* setup function to test chown for EACCES */ -int setup3(); /* setup function to test chown for ENOTDIR */ -int longpath_setup(); /* setup function to test chown for ENAMETOOLONG */ +int setup1(); /* setup function to test chown for EPERM */ +int setup2(); /* setup function to test chown for EACCES */ +int setup3(); /* setup function to test chown for ENOTDIR */ +int longpath_setup(); /* setup function to test chown for ENAMETOOLONG */ char *get_high_address(); /* Function from ltp-lib */ @@ -221,12 +221,13 @@ int main(int ac, char **av) cleanup(); return 0; - /*NOTREACHED*/} /* End main */ +/*NOTREACHED*/ +} /* End main */ /* * void * setup(void) - performs all ONE TIME setup for this test. - * Exit the test program on receipt of unexpected signals. + * Exit the test program on receipt of unexpected signals. * Create a temporary directory and change directory to it. * Invoke individual test setup functions according to the order * set in struct. definition. @@ -296,47 +297,38 @@ int no_setup() */ int setup1() { - int fd; /* file handle for testfile */ - char Path_name[PATH_MAX]; /* Buffer to hold command string */ - char Cmd_buffer[BUFSIZ]; /* Buffer to hold command string */ + int fd; /* file handle for testfile */ + uid_t old_uid; + + old_uid = geteuid(); - if ((fd = open(TEST_FILE1, O_RDWR | O_CREAT, 0666)) == -1) { + fd = open(TEST_FILE1, O_RDWR|O_CREAT, 0666); + if (fd == -1) { tst_brkm(TBROK, cleanup, "open(%s, O_RDWR|O_CREAT, 0666) failed, errno=%d : %s", TEST_FILE1, errno, strerror(errno)); } - if (close(fd) == -1) { - tst_brkm(TBROK, cleanup, - "close(%s) Failed, errno=%d : %s", - TEST_FILE1, errno, strerror(errno)); - } - /* Get the current working directory of the process */ - if (getcwd(Path_name, sizeof(Path_name)) == NULL) { + seteuid(0); + if (fchown(fd, 0, 0) < 0) tst_brkm(TBROK, cleanup, - "getcwd(3) fails to get working directory of process"); - } - /* Get the path of test file created under temporary directory */ - strcat(Path_name, "/" TEST_FILE1); + "Fail to modify %s ownership(s)!", TEST_FILE1); - /* Get the command name to be executed as setuid to root */ - strcpy((char *)Cmd_buffer, (const char *)test_home); - strcat((char *)Cmd_buffer, (const char *)"/change_owner "); - strcat((char *)Cmd_buffer, TCID); - strcat((char *)Cmd_buffer, " "); - strcat((char *)Cmd_buffer, Path_name); + seteuid(old_uid); - if (system((const char *)Cmd_buffer) != 0) { + if (close(fd) == -1) { tst_brkm(TBROK, cleanup, - "Fail to modify %s ownership(s)!", TEST_FILE1); + "close(%s) Failed, errno=%d : %s", + TEST_FILE1, errno, strerror(errno)); } + return 0; } /* * int * setup2() - setup function for a test condition for which chown(2) - * returns -1 and sets errno to EACCES. + * returns -1 and sets errno to EACCES. * Create a test directory under temporary directory and create a test file * under this directory with mode "0666" permissions. * Modify the mode permissions on test directory such that process will not @@ -376,7 +368,7 @@ int setup2() /* * int * setup3() - setup function for a test condition for which chown(2) - * returns -1 and sets errno to ENOTDIR. + * returns -1 and sets errno to ENOTDIR. * * Create a test file under temporary directory so that test tries to * change mode of a testfile "tfile_3" under "t_file" which happens to be @@ -421,10 +413,10 @@ int longpath_setup() * void * cleanup() - Performs all ONE TIME cleanup for this test at * completion or premature exit. - * Print test timing stats and errno log if test executed with options. - * Remove temporary directory and sub-directories/files under it - * created during setup(). - * Exit the test program with normal exit code. + * Print test timing stats and errno log if test executed with options. + * Remove temporary directory and sub-directories/files under it + * created during setup(). + * Exit the test program with normal exit code. */ void cleanup() { diff --git a/testcases/kernel/syscalls/fchmod/Makefile b/testcases/kernel/syscalls/fchmod/Makefile index dfa3b86..c3f60c5 100644 --- a/testcases/kernel/syscalls/fchmod/Makefile +++ b/testcases/kernel/syscalls/fchmod/Makefile @@ -24,13 +24,9 @@ TARGETS = $(patsubst %.c,%,$(SRCS)) all: $(TARGETS) -install: change_owner.mode +install: @set -e; for i in $(TARGETS); do ln -f $$i ../../../bin/$$i ; done clean: rm -f $(TARGETS) -change_owner.mode: - @/bin/sh ./change_owner.mode.sh - -.PHONY: change_owner.mode diff --git a/testcases/kernel/syscalls/fchmod/change_owner.c b/testcases/kernel/syscalls/fchmod/change_owner.c deleted file mode 100644 index 5ceccf7..0000000 --- a/testcases/kernel/syscalls/fchmod/change_owner.c +++ /dev/null @@ -1,94 +0,0 @@ -/* - * - * Copyright (c) International Business Machines Corp., 2001 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See - * the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Description: This is a setuid to root program invoked by a non-root - * process to change the user id/group id bits on the test - * directory/file created in the setup function. - * - * This function exit with 0 or 1 depending upon the - * success/failure of chown(2) system call. - */ -#include <stdio.h> -#include <unistd.h> -#include <sys/types.h> -#include <pwd.h> -#include <grp.h> -#include <errno.h> -#include <stdlib.h> -#include <string.h> - -int main(int argc, char **argv) -{ - struct passwd *ltpuser; /* password struct for ltpuser2 */ - struct group *ltpgroup; /* group struct for ltpuser2 */ - uid_t user_uid; /* user id of ltpuser2 */ - gid_t group_gid; /* group id of ltpuser2 */ - char *test_name; /* test specific name */ - char *path_name; /* name of test directory/file */ - - if (argc != 3) { - fprintf(stderr, - "This is a helper binary meant for internal LTP usage only\n"); - exit(1); - } - - test_name = argv[1]; - path_name = argv[2]; - - /* - * Get the user id and group id of "ltpuser2" user from password - * and group files. - */ - if ((ltpuser = getpwnam("nobody")) == NULL) { - perror("change_owner: nobody not found in /etc/passwd"); - exit(1); - } - if ((ltpgroup = getgrnam("nobody")) == NULL) { - if ((ltpgroup = getgrnam("nogroup")) == NULL) { - perror - ("change_owner: nobody/nogroup's group not found in /etc/group"); - exit(1); - } - } - - user_uid = 0; - group_gid = 0; - - /* Check for test specific name and set uid/gid accordingly */ - if (!(strcmp(test_name, "fchown03"))) { - user_uid = -1; - group_gid = ltpgroup->gr_gid; - } else if (!(strcmp(test_name, "fchown04"))) { - user_uid = ltpuser->pw_uid; - group_gid = ltpgroup->gr_gid; - } - - /* - * Change the ownership of test directory/file specified by - * pathname to that of user_uid and group_gid. - */ - if (chown(path_name, user_uid, group_gid) < 0) { - fprintf(stderr, "change_owner: chown() of %s failed, error " - "%d\n", path_name, errno); - exit(1); - } - - exit(0); -} diff --git a/testcases/kernel/syscalls/fchmod/change_owner.mode.sh b/testcases/kernel/syscalls/fchmod/change_owner.mode.sh deleted file mode 100644 index 05f1240..0000000 --- a/testcases/kernel/syscalls/fchmod/change_owner.mode.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/sh - -failed() { - echo "" - echo " ************** WARNING **************" - echo " Cannot change permission or ownership of \"change_owner\"." - echo " Tests in this directory will fail"$ - echo " Run "make install" as root." - echo " *************************************" - sleep 2 -} - -chown root change_owner || failed -chmod 04755 change_owner || failed - -exit 0 diff --git a/testcases/kernel/syscalls/fchmod/fchmod05.c b/testcases/kernel/syscalls/fchmod/fchmod05.c index 244be45..3573f65 100644 --- a/testcases/kernel/syscalls/fchmod/fchmod05.c +++ b/testcases/kernel/syscalls/fchmod/fchmod05.c @@ -42,11 +42,11 @@ * Loop if the proper options are given. * Execute system call * Check return code, if system call failed (return=-1) - * Log the errno and Issue a FAIL message. + * Log the errno and Issue a FAIL message. * Otherwise, - * Verify the Functionality of system call + * Verify the Functionality of system call * if successful, - * Issue Functionality-Pass message. + * Issue Functionality-Pass message. * Otherwise, * Issue Functionality-Fail message. * Cleanup: @@ -89,7 +89,7 @@ #include "test.h" #include "usctest.h" -#define MODE_RWX S_IRWXU | S_IRWXG | S_IRWXO +#define MODE_RWX (S_IRWXU | S_IRWXG | S_IRWXO) #define PERMS 043777 #define TESTDIR "testdir" @@ -151,7 +151,7 @@ int main(int ac, char **av) TESTDIR, TEST_ERRNO); } dir_mode = stat_buf.st_mode; - if (PERMS != dir_mode) { + if ((PERMS & ~S_ISGID) != dir_mode) { tst_resm(TFAIL, "%s: Incorrect modes 0%03o, " "Expected 0%03o", TESTDIR, PERMS, MODE_RWX); @@ -181,9 +181,8 @@ int main(int ac, char **av) */ void setup() { - char *test_home; /* variable to hold TESTHOME env */ - char Path_name[PATH_MAX]; /* Buffer to hold command string */ - char Cmd_buffer[BUFSIZ]; /* Buffer to hold command string */ + struct passwd *nobody_u; + struct group *bin_group; /* capture signals */ tst_sig(FORK, DEF_HANDLER, cleanup); @@ -194,14 +193,22 @@ void setup() tst_exit(); } - test_home = get_current_dir_name(); - /* Pause if that option was specified */ TEST_PAUSE; /* make a temp directory and cd to it */ tst_tmpdir(); + nobody_u = getpwnam("nobody"); + if (!nobody_u) + tst_brkm(TBROK, cleanup, + "Couldn't find uid of nobody: %s", strerror(errno)); + + bin_group = getgrnam("bin"); + if (!bin_group) + tst_brkm(TBROK, cleanup, + "Couldn't find gid of bin: %s", strerror(errno)); + /* * Create a test directory under temporary directory with specified * mode permissions and change the gid of test directory to that of @@ -211,32 +218,18 @@ void setup() tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", TESTDIR); } - /* Get the current working directory of the process */ - if (getcwd(Path_name, sizeof(Path_name)) == NULL) { - tst_brkm(TBROK, cleanup, - "getcwd(3) fails to get working directory of process"); - } + if (chown(TESTDIR, nobody_u->pw_uid, bin_group->gr_gid) == -1) + tst_brkm(TBROK, cleanup, "Couldn't change owner of testdir: %s", + strerror(errno)); - /* - * Get the complete path of TESTDIR created - * under temporary directory - */ - strcat(Path_name, "/" TESTDIR); - - /* Get the command name to be executed as setuid to root */ - strcpy((char *)Cmd_buffer, (const char *)test_home); - strcat((char *)Cmd_buffer, (const char *)"/change_owner "); - strcat((char *)Cmd_buffer, TCID); - strcat((char *)Cmd_buffer, " "); - strcat((char *)Cmd_buffer, Path_name); - - if (system((const char *)Cmd_buffer) != 0) { - tst_brkm(TBROK, cleanup, - "Fail to modify %s group ownerships", TESTDIR); - } + /* change to nobody:nobody */ + if (setegid(nobody_u->pw_gid) == -1 || seteuid(nobody_u->pw_uid) == -1) + tst_brkm(TBROK, cleanup, "Couldn't switch to nobody:nobody: %s", + strerror(errno)); /* Open the test directory for reading */ - if ((fd = open(TESTDIR, O_RDONLY)) == -1) { + fd = open(TESTDIR, O_RDONLY); + if (fd == -1) { tst_brkm(TBROK, cleanup, "open(%s, O_RDONLY) failed, errno=%d : %s", TESTDIR, errno, strerror(errno)); diff --git a/testcases/kernel/syscalls/fchmod/fchmod06.c b/testcases/kernel/syscalls/fchmod/fchmod06.c index a46199d..4cb589b 100644 --- a/testcases/kernel/syscalls/fchmod/fchmod06.c +++ b/testcases/kernel/syscalls/fchmod/fchmod06.c @@ -41,9 +41,9 @@ * Loop if the proper options are given. * Execute system call * Check return code, if system call failed (return=-1) - * if errno set == expected errno - * Issue sys call fails with expected return value and errno. - * Otherwise, + * if errno set == expected errno + * Issue sys call fails with expected return value and errno. + * Otherwise, * Issue sys call fails with unexpected errno. * Otherwise, * Issue sys call returns unexpected value. @@ -258,8 +258,7 @@ void setup() */ int setup1() { - char Path_name[PATH_MAX]; /* Buffer to hold command string */ - char Cmd_buffer[BUFSIZ]; /* Buffer to hold command string */ + uid_t old_uid; /* Create a testfile under temporary directory */ if ((fd1 = open(TEST_FILE1, O_RDWR | O_CREAT, 0666)) == -1) { @@ -268,26 +267,15 @@ int setup1() TEST_FILE1, errno, strerror(errno)); } - /* Get the current working directory of the process */ - if (getcwd(Path_name, sizeof(Path_name)) == NULL) { - tst_brkm(TBROK, cleanup, - "getcwd(3) fails to get working directory of process"); - } + old_uid = geteuid(); + seteuid(0); - /* Get the path of test file created under temporary directory */ - strcat(Path_name, "/" TEST_FILE1); + if (fchown(fd1, 0, 0) < 0) + tst_brkm(TBROK, cleanup, "Fail to modify %s ownership(s): %s", + TEST_FILE1, strerror(errno)); - /* Get the command name to be executed as setuid to root */ - strcpy((char *)Cmd_buffer, (const char *)test_home); - strcat((char *)Cmd_buffer, "/change_owner "); - strcat((char *)Cmd_buffer, TCID); - strcat((char *)Cmd_buffer, " "); - strcat((char *)Cmd_buffer, Path_name); + seteuid(old_uid); - if (system((const char *)Cmd_buffer) != 0) { - tst_brkm(TBROK, cleanup, - "Fail to modify %s ownership(s)!", TEST_FILE1); - } return 0; } diff --git a/testcases/kernel/syscalls/fchown/Makefile b/testcases/kernel/syscalls/fchown/Makefile index 0e930e5..1ecc0ae 100644 --- a/testcases/kernel/syscalls/fchown/Makefile +++ b/testcases/kernel/syscalls/fchown/Makefile @@ -26,13 +26,9 @@ TARGETS += $(patsubst %.c,%,$(SRCS)) all: $(TARGETS) -install: change_owner.mode +install: @set -e; for i in $(TARGETS); do ln -f $$i ../../../bin/$$i ; done clean: rm -f $(TARGETS) -change_owner.mode: - @/bin/sh ./change_owner.mode.sh - -.PHONY: change_owner.mode diff --git a/testcases/kernel/syscalls/fchown/change_owner.c b/testcases/kernel/syscalls/fchown/change_owner.c deleted file mode 100644 index 5ceccf7..0000000 --- a/testcases/kernel/syscalls/fchown/change_owner.c +++ /dev/null @@ -1,94 +0,0 @@ -/* - * - * Copyright (c) International Business Machines Corp., 2001 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See - * the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Description: This is a setuid to root program invoked by a non-root - * process to change the user id/group id bits on the test - * directory/file created in the setup function. - * - * This function exit with 0 or 1 depending upon the - * success/failure of chown(2) system call. - */ -#include <stdio.h> -#include <unistd.h> -#include <sys/types.h> -#include <pwd.h> -#include <grp.h> -#include <errno.h> -#include <stdlib.h> -#include <string.h> - -int main(int argc, char **argv) -{ - struct passwd *ltpuser; /* password struct for ltpuser2 */ - struct group *ltpgroup; /* group struct for ltpuser2 */ - uid_t user_uid; /* user id of ltpuser2 */ - gid_t group_gid; /* group id of ltpuser2 */ - char *test_name; /* test specific name */ - char *path_name; /* name of test directory/file */ - - if (argc != 3) { - fprintf(stderr, - "This is a helper binary meant for internal LTP usage only\n"); - exit(1); - } - - test_name = argv[1]; - path_name = argv[2]; - - /* - * Get the user id and group id of "ltpuser2" user from password - * and group files. - */ - if ((ltpuser = getpwnam("nobody")) == NULL) { - perror("change_owner: nobody not found in /etc/passwd"); - exit(1); - } - if ((ltpgroup = getgrnam("nobody")) == NULL) { - if ((ltpgroup = getgrnam("nogroup")) == NULL) { - perror - ("change_owner: nobody/nogroup's group not found in /etc/group"); - exit(1); - } - } - - user_uid = 0; - group_gid = 0; - - /* Check for test specific name and set uid/gid accordingly */ - if (!(strcmp(test_name, "fchown03"))) { - user_uid = -1; - group_gid = ltpgroup->gr_gid; - } else if (!(strcmp(test_name, "fchown04"))) { - user_uid = ltpuser->pw_uid; - group_gid = ltpgroup->gr_gid; - } - - /* - * Change the ownership of test directory/file specified by - * pathname to that of user_uid and group_gid. - */ - if (chown(path_name, user_uid, group_gid) < 0) { - fprintf(stderr, "change_owner: chown() of %s failed, error " - "%d\n", path_name, errno); - exit(1); - } - - exit(0); -} diff --git a/testcases/kernel/syscalls/fchown/change_owner.mode.sh b/testcases/kernel/syscalls/fchown/change_owner.mode.sh deleted file mode 100644 index 05f1240..0000000 --- a/testcases/kernel/syscalls/fchown/change_owner.mode.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/sh - -failed() { - echo "" - echo " ************** WARNING **************" - echo " Cannot change permission or ownership of \"change_owner\"." - echo " Tests in this directory will fail"$ - echo " Run "make install" as root." - echo " *************************************" - sleep 2 -} - -chown root change_owner || failed -chmod 04755 change_owner || failed - -exit 0 diff --git a/testcases/kernel/syscalls/fchown/fchown03.c b/testcases/kernel/syscalls/fchown/fchown03.c index 6153934..c579a5a 100644 --- a/testcases/kernel/syscalls/fchown/fchown03.c +++ b/testcases/kernel/syscalls/fchown/fchown03.c @@ -42,11 +42,11 @@ * Loop if the proper options are given. * Execute system call * Check return code, if system call failed (return=-1) - * Log the errno and Issue a FAIL message. + * Log the errno and Issue a FAIL message. * Otherwise, - * Verify the Functionality of system call + * Verify the Functionality of system call * if successful, - * Issue Functionality-Pass message. + * Issue Functionality-Pass message. * Otherwise, * Issue Functionality-Fail message. * Cleanup: @@ -83,13 +83,13 @@ #include "test.h" #include "usctest.h" -#define FILE_MODE (mode_t)S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH -#define NEW_PERMS (mode_t)S_IFREG | S_IRWXU | S_IRWXG | S_ISUID | S_ISGID -#define FCHOWN_PERMS (mode_t)S_IFREG | S_IRWXU | S_IRWXG +#define FILE_MODE (mode_t)(S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) +#define NEW_PERMS (mode_t)(S_IFREG | S_IRWXU | S_IRWXG | S_ISUID | S_ISGID) +#define FCHOWN_PERMS (mode_t)(NEW_PERMS & ~(S_ISUID | S_ISGID)) #define TESTFILE "testfile" int fildes; /* File descriptor for test file */ -char *TCID = "fchown03"; /* Test program identifier. */ +char *TCID = "fchown03"; /* Test program identifier. */ int TST_TOTAL = 1; /* Total number of test conditions */ extern int Tst_count; /* Test Case counter for tst_* routines */ char nobody_uid[] = "nobody"; @@ -169,7 +169,7 @@ int main(int ac, char **av) * set on the testfile in setup() are * cleared by fchown() */ - if (stat_buf.st_mode & (S_ISUID | S_ISGID)) { + if (stat_buf.st_mode != FCHOWN_PERMS) { tst_resm(TFAIL, "%s: Incorrect mode permissions" " %#o, Expected %#o", TESTFILE, stat_buf.st_mode, FCHOWN_PERMS); @@ -196,9 +196,6 @@ int main(int ac, char **av) */ void setup() { - char test_home[PATH_MAX]; /* variable to hold TESTHOME env */ - char Path_name[PATH_MAX]; /* Buffer to hold command string */ - char Cmd_buffer[BUFSIZ]; /* Buffer to hold command string */ /* capture signals */ tst_sig(FORK, DEF_HANDLER, cleanup); @@ -209,14 +206,9 @@ void setup() } ltpuser = getpwnam(nobody_uid); if (seteuid(ltpuser->pw_uid) == -1) { - tst_resm(TINFO, "seteuid failed to " - "to set the effective uid to %d", ltpuser->pw_uid); - perror("seteuid"); - } - - if (getcwd(test_home, sizeof(test_home)) == NULL) { - tst_brkm(TBROK, cleanup, - "getcwd(3) fails to get working directory of process"); + tst_brkm(TBROK, cleanup, "seteuid failed to " + "to set the effective uid to %d: %s", ltpuser->pw_uid, + strerror(errno)); } /* Pause if that option was specified */ @@ -231,35 +223,18 @@ void setup() "open(%s, O_RDWR|O_CREAT, %o) Failed, errno=%d : %s", TESTFILE, FILE_MODE, errno, strerror(errno)); } + seteuid(0); + if (fchown(fildes, -1, 0) < 0) + tst_brkm(TBROK, cleanup, "Fail to modify Ownership of %s: %s", + TESTFILE, strerror(errno)); - /* - * Change mode permissions on testfile such that - * setuid/setgid bits are set on the testfile. - */ - if (chmod(TESTFILE, NEW_PERMS) < 0) { - tst_brkm(TBROK, cleanup, "chmod(2) on %s Failed, errno=%d : %s", - TESTFILE, errno, strerror(errno)); - } - - /* Get the current working directory of the process */ - if (getcwd(Path_name, sizeof(Path_name)) == NULL) { - tst_brkm(TBROK, cleanup, - "getcwd(3) fails to get working directory of process"); - } - /* Get the path of TESTFILE under temporary directory */ - strcat(Path_name, "/" TESTFILE); + if (fchmod(fildes, NEW_PERMS) < 0) + tst_brkm(TBROK, cleanup, "Fail to modify Mode of %s: %s", + TESTFILE, strerror(errno)); - /* Get the command name to be executed as setuid to root */ - strcpy((char *)Cmd_buffer, (const char *)test_home); - strcat((char *)Cmd_buffer, (const char *)"/change_owner "); - strcat((char *)Cmd_buffer, TCID); - strcat((char *)Cmd_buffer, " "); - strcat((char *)Cmd_buffer, Path_name); + setegid(ltpuser->pw_gid); + seteuid(ltpuser->pw_uid); - if (system((const char *)Cmd_buffer) != 0) { - tst_brkm(TBROK, cleanup, - "Fail to modify Ownership of %s", TESTFILE); - } } /* End setup() */ /* diff --git a/testcases/kernel/syscalls/fchown/fchown04.c b/testcases/kernel/syscalls/fchown/fchown04.c index 35dc6b4..0980d44 100644 --- a/testcases/kernel/syscalls/fchown/fchown04.c +++ b/testcases/kernel/syscalls/fchown/fchown04.c @@ -41,9 +41,9 @@ * Loop if the proper options are given. * Execute system call * Check return code, if system call failed (return=-1) - * if errno set == expected errno - * Issue sys call fails with expected return value and errno. - * Otherwise, + * if errno set == expected errno + * Issue sys call fails with expected return value and errno. + * Otherwise, * Issue sys call fails with unexpected errno. * Otherwise, * Issue sys call returns unexpected value. @@ -205,7 +205,7 @@ int main(int ac, char **av) /* * setup(void) - performs all ONE TIME setup for this test. * - * Exit the test program on receipt of unexpected signals. + * Exit the test program on receipt of unexpected signals. * Create a temporary directory and change directory to it. * Invoke individual test setup functions according to the order * set in struct. definition. @@ -222,15 +222,13 @@ void setup() /* Pause if that option was specified */ TEST_PAUSE; -//changed by prashant yendigeri because the temp directory creating and deletinI// are with different uids, so this test case will fail in most of the scenario. +/* + * changed by prashant yendigeri because the temp directory creating and + * deleting are with different uids, so this test case will fail in most of + * the scenario. + */ /* Make a temp dir and cd to it */ -// tst_tmpdir(); - - /* Get the current working directory of the process */ - if (getcwd(test_home, sizeof(test_home)) == NULL) { - tst_brkm(TBROK, cleanup, - "getcwd(3) fails to get working directory of process"); - } + /* tst_tmpdir(); */ /* Switch to bin user for correct error code collection */ if (geteuid() != 0) { @@ -275,43 +273,31 @@ void setup() */ int setup1() { - char Path_name[PATH_MAX]; /* Buffer to hold command string */ - char Cmd_buffer[BUFSIZ]; /* Buffer to hold command string */ - char *change_owner_path; + int old_uid; + struct passwd *nobody; /* Create a testfile under temporary directory */ - if ((fd1 = open(TEST_FILE1, O_RDWR | O_CREAT, 0666)) == -1) { + fd1 = open(TEST_FILE1, O_RDWR | O_CREAT, 0666); + if (fd1 == -1) { tst_brkm(TBROK, cleanup, "open(%s, O_RDWR|O_CREAT, 0666) failed, errno=%d : %s", TEST_FILE1, errno, strerror(errno)); } - /* Get the current working directory of the process */ - if (getcwd(Path_name, sizeof(Path_name)) == NULL) { - tst_brkm(TBROK, cleanup, - "getcwd(3) fails to get working directory of process"); - } - /* Get the path of test file created under temporary directory */ - strcat(Path_name, "/" TEST_FILE1); + old_uid = geteuid(); + seteuid(0); - /* Set the environment variable change_owner if not already set */ - setenv("change_owner", strcat(test_home, "/change_owner"), 0); + nobody = getpwnam("nobody"); + if (!nobody) + tst_brkm(TBROK, cleanup, "Couldn't find user nobody: %s", + strerror(errno)); - /* Get the command name to be executed as setuid to root */ - if ((change_owner_path = getenv("change_owner")) == NULL) { - tst_brkm(TBROK, cleanup, - "getenv() failed to get the cmd to be execd as setuid to root"); - } - strcpy((char *)Cmd_buffer, (const char *)change_owner_path); - strcat((char *)Cmd_buffer, " "); - strcat((char *)Cmd_buffer, TCID); - strcat((char *)Cmd_buffer, " "); - strcat((char *)Cmd_buffer, Path_name); + if (fchown(fd1, nobody->pw_uid, nobody->pw_gid) < 0) + tst_brkm(TBROK, cleanup, "Fail to modify %s ownership(s)! %s", + TEST_FILE1, strerror(errno)); + + seteuid(old_uid); - if (system((const char *)Cmd_buffer) != 0) { - tst_brkm(TBROK, cleanup, - "Fail to modify %s ownership(s)!", TEST_FILE1); - } return 0; } -- 1.5.5.1 |