Hello,
this is the patch for removal of change_owner. I modified the patch to fix some
problems with my older patch, mostly that the setegid() calls actually didn't
work. Also, it fixes some long lasting breakage (chmod05 never giving up root,
STD_FUNCTIONAL_TEST set to 0 ...), mostly done according to test comments.
Regards
Jiri Palecek
Signed-off-by: Jiri Palecek <[email protected]>
---
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 | 59 +++++--------
testcases/kernel/syscalls/chmod/chmod06.c | 28 +-----
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 | 73 ++++-----------
testcases/kernel/syscalls/chown/chown04.c | 36 +++-----
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 | 45 ++++------
testcases/kernel/syscalls/fchmod/fchmod06.c | 26 ++----
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 | 54 +++--------
testcases/kernel/syscalls/fchown/fchown04.c | 42 +++-------
20 files changed, 116 insertions(+), 711 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/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..2b6af7d 100644
--- a/testcases/kernel/syscalls/chmod/chmod05.c
+++ b/testcases/kernel/syscalls/chmod/chmod05.c
@@ -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,48 +214,39 @@ 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();
- /*
- * Create a test directory under temporary directory with specified
- * mode permissions and change the gid of test directory to that of
- //wjh Improper comment! Ownership it changed to "nobody"
- * guest user2.
- */
- if (mkdir(TESTDIR, MODE_RWX) < 0) {
- tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", TESTDIR);
- }
+ if(!nobody_u=getpwnam("nobody"))
+ tst_brkm(TBROK, cleanup,
+ "Couldn't find uid of nobody:
%s", strerror(errno));
- /* Get the current working directory of the process */
- if (getcwd(Path_name, sizeof(Path_name)) == NULL) {
+ if(!bin_group=getgrnam("bin"))
tst_brkm(TBROK, cleanup,
- "getcwd(3) fails to get working directory of process");
- }
+ "Couldn't find gid of bin:
%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);
+ * Create a test directory under temporary directory with specified
+ * mode permissions and change the gid of test directory to that of
+//wjh Improper comment! Ownership it changed to "nobody"
+ * guest user2.
+ */
+ if (mkdir(TESTDIR, MODE_RWX) < 0) {
+ tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed: %s", TESTDIR,
strerror(errno));
+ }
- if (system((const char *)Cmd_buffer) != 0) {
+ if(chown(TESTDIR, nobody_u->pw_uid, bin_group->gr_gid) == -1)
tst_brkm(TBROK, cleanup,
- "Fail to modify %s group ownership", TESTDIR);
- }
+ "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..df00106 100644
--- a/testcases/kernel/syscalls/chmod/chmod06.c
+++ b/testcases/kernel/syscalls/chmod/chmod06.c
@@ -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..1fc99ce 100644
--- a/testcases/kernel/syscalls/chown/chown03.c
+++ b/testcases/kernel/syscalls/chown/chown03.c
@@ -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) */
@@ -169,10 +165,10 @@ 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);
@@ -197,11 +193,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 +222,35 @@ 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));
- }
- /* 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(seteuid(0)==-1)
+ tst_brkm(TBROK, cleanup,
+ "Couldn't switch to
user root: %s", strerror(errno));
- /* Set the environment variable change_owner if not already set */
- setenv("change_owner", strcat(test_home, "/change_owner"), 0);
+ if(fchown(fd, -1, 0)<0)
+ tst_brkm(TBROK, cleanup,
+ "Fail to modify Ownership 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) {
+ if(fchmod(fd, NEW_PERMS)<0)
tst_brkm(TBROK, cleanup,
- "getenv() failed to get the cmd to be execd as setuid
to root");
- }
+ "Fail to modify Mode of %s:
%s", TESTFILE, 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(seteuid(ltpuser->pw_uid)==-1)
+ tst_brkm(TBROK, cleanup,
+ "Couldn't switch to
user nobody: %s", strerror(errno));
- if (system((const char *)Cmd_buffer) != 0) {
+ /* Close the test file created above */
+ if (close(fd) == -1) {
tst_brkm(TBROK, cleanup,
- "Fail to modify Ownership of %s", TESTFILE);
+ "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..c3b3831 100644
--- a/testcases/kernel/syscalls/chown/chown04.c
+++ b/testcases/kernel/syscalls/chown/chown04.c
@@ -296,40 +296,30 @@ 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) {
+ if ((fd = open(TEST_FILE1, O_RDWR|O_CREAT, 0666)) == -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;
}
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..f27df39 100644
--- a/testcases/kernel/syscalls/fchmod/fchmod05.c
+++ b/testcases/kernel/syscalls/fchmod/fchmod05.c
@@ -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,29 +218,15 @@ 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) {
+ if(chown(TESTDIR, nobody_u->pw_uid, bin_group->gr_gid) == -1)
tst_brkm(TBROK, cleanup,
- "getcwd(3) fails to get working directory of process");
- }
+ "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) {
+ // change to nobody:nobody
+ if(setegid(nobody_u->pw_gid)==-1 ||
+ seteuid(nobody_u->pw_uid)==-1)
tst_brkm(TBROK, cleanup,
- "Fail to modify %s group ownerships", TESTDIR);
- }
+ "Couldn't switch to
nobody:nobody: %s", strerror(errno));
/* Open the test directory for reading */
if ((fd = open(TESTDIR, O_RDONLY)) == -1) {
diff --git a/testcases/kernel/syscalls/fchmod/fchmod06.c
b/testcases/kernel/syscalls/fchmod/fchmod06.c
index a46199d..2e851be 100644
--- a/testcases/kernel/syscalls/fchmod/fchmod06.c
+++ b/testcases/kernel/syscalls/fchmod/fchmod06.c
@@ -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..75bb971 100644
--- a/testcases/kernel/syscalls/fchown/fchown03.c
+++ b/testcases/kernel/syscalls/fchown/fchown03.c
@@ -83,9 +83,9 @@
#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 */
@@ -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,8 @@ 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 +222,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) {
+ if(fchmod(fildes, NEW_PERMS)<0)
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);
+ "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..a368cd8 100644
--- a/testcases/kernel/syscalls/fchown/fchown04.c
+++ b/testcases/kernel/syscalls/fchown/fchown04.c
@@ -226,12 +226,6 @@ void setup()
/* 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");
- }
-
/* Switch to bin user for correct error code collection */
if (geteuid() != 0) {
tst_brkm(TBROK, cleanup, "Test must be run as root");
@@ -275,9 +269,8 @@ 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) {
@@ -286,32 +279,19 @@ 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");
- }
- /* Get the path of test file created under temporary directory */
- strcat(Path_name, "/" TEST_FILE1);
-
- /* Set the environment variable change_owner if not already set */
- setenv("change_owner", strcat(test_home, "/change_owner"), 0);
+ old_uid=geteuid();
+ seteuid(0);
- /* Get the command name to be executed as setuid to root */
- if ((change_owner_path = getenv("change_owner")) == NULL) {
+ if(!(nobody=getpwnam("nobody")))
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);
+ "Couldn't find user nobody:
%s", strerror(errno));
- if (system((const char *)Cmd_buffer) != 0) {
+ if(fchown(fd1, nobody->pw_uid, nobody->pw_gid)<0)
tst_brkm(TBROK, cleanup,
- "Fail to modify %s ownership(s)!", TEST_FILE1);
- }
+ "Fail to modify %s
ownership(s)! %s", TEST_FILE1, strerror(errno));
+
+ seteuid(old_uid);
+
return 0;
}
--
1.6.2
------------------------------------------------------------------------------
Stay on top of everything new and different, both inside and
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today.
Use priority code J9JMT32. http://p.sf.net/sfu/p
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list