syscalls/fchown: Convert fchown{04, 05} to the new API
cover syscalls/fchown: Convert fchown{04, 05} to the new API
1. Cleanup and convert fchown{04, 05} to the new API.
2. Remove duplicate test points for fchown05.
Xie Ziyao (2):
syscalls/fchown: Convert fchown04 to the new API syscalls/fchown: Convert fchown05 to the new API
testcases/kernel/syscalls/fchown/fchown04.c | 179 +++++--------------- testcases/kernel/syscalls/fchown/fchown05.c | 147 +++++----------- 2 files changed, 87 insertions(+), 239 deletions(-)
--
2.17.1
PATCH 1/2 syscalls/fchown: Convert fchown04 to the new API
Cleanup and convert fchown04 to the new API.
--- testcases/kernel/syscalls/fchown/fchown04.c | 179 +++++--------------- 1 file changed, 47 insertions(+), 132 deletions(-)
testcases/kernel/syscalls/fchown/fchown04.c +47 -132
@@ -1,168 +1,83 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* -* Copyright (c) International Business Machines Corp., 2001 -* 07/2001 Ported by Wayne Boyer -* -* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +* Copyright (c) International Business Machines Corp., 2001 +* 07/2001 Ported by Wayne Boyer +* Copyright (c) 2021 Xie Ziyao <[email protected]> */ -/* -* Test Description: -* Verify that, -* 1) fchown(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. -* 2) fchown(2) returns -1 and sets errno to EBADF if the file descriptor -* of the specified file is not valid. -* 3) fchown(2) returns -1 and sets errno to EROFS if the named file resides -* on a read-only file system. +/*\ +* [Description] +* +* Verify that: +* +* 1. fchown() 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. +* 2. fchown() returns -1 and sets errno to EBADF if the file descriptor +* of the specified file is not valid. +* 3. fchown() returns -1 and sets errno to EROFS if the named file resides +* on a read-only file system. */ -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <fcntl.h> -#include <errno.h> -#include <string.h> -#include <signal.h> -#include <grp.h> #include <pwd.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <sys/mount.h> -#include "test.h" -#include "safe_macros.h" -#include "compat_16.h" +#include "tst_test.h" +#include "compat_tst_16.h" +#include "tst_safe_macros.h" -#define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \ - S_IXGRP|S_IROTH|S_IXOTH) +#define MNT_POINT "mntpoint" +#define TEST_FILE "tfile_1" +#define MODE 0666 +#define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)static int fd1;static int fd2 =-1 ;static int fd3; -static const char *device; -static int mount_flag;static struct test_case_t {int *fd;int exp_errno; -} test_cases[] = { +} tc[] = { {&fd1,EPERM }, {&fd2,EBADF }, {&fd3,EROFS }, }; -TCID_DEFINE (fchown04); -int TST_TOTAL =ARRAY_SIZE (test_cases); - -static void setup (void ); -static void fchown_verify (int ); -static void cleanup (void ); - -int main (int ac,char **av) -{ -int lc; -int i; - -tst_parse_opts (ac, av,NULL ,NULL ); - -setup (); - -for (lc =0 ;TEST_LOOPING (lc); lc++) { - - tst_count =0 ; - -for (i =0 ; i <TST_TOTAL ; i++) -fchown_verify (i); - } - -cleanup (); -tst_exit (); -} -static void setup (void ) {struct passwd *ltpuser; -const char *fs_type; - -TEST_PAUSE ; - -tst_require_root (); - -tst_tmpdir (); - - fs_type =tst_dev_fs_type (); - device =tst_acquire_device (cleanup); - -if (!device) -tst_brkm (TCONF , cleanup,"Failed to acquire device" ); - -tst_sig (NOFORK ,DEF_HANDLER , cleanup); - - fd1 =SAFE_OPEN (cleanup,"tfile_1" ,O_RDWR |O_CREAT ,0666 ); -tst_mkfs (cleanup, device, fs_type,NULL ,NULL ); -SAFE_MKDIR (cleanup,"mntpoint" ,DIR_MODE ); -SAFE_MOUNT (cleanup, device,"mntpoint" , fs_type,0 ,NULL ); - mount_flag =1 ; -SAFE_TOUCH (cleanup,"mntpoint/tfile_3" ,0644 ,NULL ); -SAFE_MOUNT (cleanup, device,"mntpoint" , fs_type, -MS_REMOUNT |MS_RDONLY ,NULL ); - fd3 =SAFE_OPEN (cleanup,"mntpoint/tfile_3" ,O_RDONLY ); + fd1 =SAFE_OPEN (TEST_FILE ,O_RDWR |O_CREAT ,MODE ); + fd3 =SAFE_OPEN (MNT_POINT ,O_RDONLY ); - ltpuser =SAFE_GETPWNAM (cleanup,"nobody" ); -SAFE_SETEUID (cleanup, ltpuser->pw_uid); + ltpuser =SAFE_GETPWNAM ("nobody" ); +SAFE_SETEUID (ltpuser->pw_uid); } -static void fchown_verify (int i) +static void run (unsigned int i) { - UID16_CHECK(geteuid(), "fchown", cleanup) - GID16_CHECK(getegid(), "fchown", cleanup) +uid_t uid; +gid_t gid; -TEST (FCHOWN (cleanup, *test_cases[i].fd,geteuid (),getegid ())); +UID16_CHECK ((uid =geteuid ()),"fchown" ); +GID16_CHECK ((gid =getegid ()),"fchown" ); -if (TEST_RETURN ==-1 ) { -if (TEST_ERRNO == test_cases[i].exp_errno) { -tst_resm (TPASS |TTERRNO ,"fchown failed as expected" ); - }else { -tst_resm (TFAIL |TTERRNO , -"fchown failed unexpectedly; expected %d - %s" , - test_cases[i].exp_errno, -strerror (test_cases[i].exp_errno)); - } - }else { -tst_resm (TFAIL ,"fchown passed unexpectedly" ); - } +TST_EXP_FAIL (FCHOWN (*tc[i].fd, uid, gid), tc[i].exp_errno); }static void cleanup (void ) { -if (seteuid (0 )) -tst_resm (TWARN |TERRNO ,"Failet to seteuid(0) before cleanup" ); - -if (fd1 >0 &&close (fd1)) -tst_resm (TWARN |TERRNO ,"Failed to close fd1" ); - -if (fd3 >0 &&close (fd3)) -tst_resm (TWARN |TERRNO ,"Failed to close fd3" ); - -if (mount_flag &&tst_umount ("mntpoint" ) <0 ) -tst_resm (TWARN |TERRNO ,"umount device:%s failed" , device); - -if (device) -tst_release_device (device); - -tst_rmdir (); +SAFE_SETEUID (0 ); +SAFE_CLOSE (fd1); +SAFE_CLOSE (fd3); } + +static struct tst_test test = { + .needs_root =1 , + .needs_rofs =1 , + .mntpoint =MNT_POINT , + .tcnt =ARRAY_SIZE (tc), + .test = run, + .setup = setup, + .cleanup = cleanup, +};
--
2.17.1
PATCH 1/2 syscalls/fchown: Convert fchown04 to the new API
Hi!
Pushed with two minor changes, thanks.
* Removed the SAFE_SETEUID(0) from cleanup since it's not needed
* Added if (fd > 0) to the cleanup, since technically the test can exit with TBROK even before these filedescriptors are open so we have to do this check
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 2/2 syscalls/fchown: Convert fchown05 to the new API
In reply to Xie Ziyao1. Cleanup and convert fchown05 to the new API.
2. Remove duplicate test points.
--- testcases/kernel/syscalls/fchown/fchown05.c | 147 ++++++-------------- 1 file changed, 40 insertions(+), 107 deletions(-)
testcases/kernel/syscalls/fchown/fchown05.c +40 -107
@@ -1,132 +1,65 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) International Business Machines Corp., 2001 -* 07/2001 Ported by Wayne Boyer -* -* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ -/* -* Test Description: -* Verify that, fchown(2) succeeds to change the owner and group of a file -* specified by file descriptor to any numeric owner(uid)/group(gid) values -* when invoked by super-user. +* 07/2001 Ported by Wayne Boyer +* Copyright (c) 2021 Xie Ziyao <[email protected]> */ -#include <stdio.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <errno.h> -#include <string.h> -#include <signal.h> +/*\ +* [Description] +* +* Verify that, fchown() succeeds to change the owner and group of a file +* specified by file descriptor to any numeric owner(uid)/group(gid) values +* when invoked by super-user. +*/ -#include "test.h" -#include "safe_macros.h" -#include "compat_16.h" +#include "tst_test.h" +#include "compat_tst_16.h" +#include "tst_safe_macros.h" -#define FILE_MODE S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH -#define TESTFILE "testfile" +#define FILE_MODE (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) +#define TESTFILE "testfile" -TCID_DEFINE (fchown05); +static int fd; -static struct test_case_t { -char *desc; -uid_t user_id; -gid_t group_id; +struct test_case_t { +uid_t uid; +gid_t gid; } tc[] = { - {"Change Owner/Group ids" ,700 ,701 }, - {"Change Owner id only" ,702 ,-1 }, - {"Change Owner id only" ,703 ,701 }, - {"Change Group id only" ,-1 ,704 }, - {"Change Group id only" ,703 ,705 }, - {NULL ,0 ,0 } + {700 ,701 }, + {702 ,701 }, + {702 ,703 }, + {704 ,705 } }; -int TST_TOTAL =ARRAY_SIZE (tc); - -static void setup (void ); -static void cleanup (void ); -static int fildes; - -int main (int ac,char **av) +static void run (unsigned int i) {struct stat stat_buf; -int i, lc; -uid_t user_id; -gid_t group_id; - -tst_parse_opts (ac, av,NULL ,NULL ); - -setup (); - -for (lc =0 ;TEST_LOOPING (lc); lc++) { - tst_count =0 ; - -for (i =0 ; tc[i].desc !=NULL ; i++) { - user_id = tc[i].user_id; - group_id = tc[i].group_id; -TEST (FCHOWN (cleanup, fildes, user_id, group_id)); +TST_EXP_PASS (FCHOWN (fd, tc[i].uid, tc[i].gid)); -if (TEST_RETURN ==-1 ) { -tst_resm (TFAIL |TTERRNO , -"fchown() Fails to %s" , tc[i].desc); -continue ; - } - -SAFE_FSTAT (cleanup, fildes, &stat_buf); - -if (user_id == (uid_t)-1 ) - user_id = tc[i -1 ].user_id; - -if (group_id == (gid_t)-1 ) - group_id = tc[i -1 ].group_id; - -if ((stat_buf.st_uid != user_id) || - (stat_buf.st_gid != group_id)) { -tst_resm (TFAIL ,"%s: Incorrect owner" -"ship set, Expected %d %d" , -TESTFILE , user_id, group_id); - }else { -tst_resm (TPASS , -"fchown() succeeds to %s of %s" , - tc[i].desc,TESTFILE ); - } - } +SAFE_FSTAT (fd, &stat_buf); +if (stat_buf.st_uid != tc[i].uid || stat_buf.st_gid != tc[i].gid) { +tst_res (TFAIL ,"%s: incorrect ownership set, expected %d %d" , +TESTFILE , tc[i].uid, tc[i].gid); } - -cleanup (); -tst_exit (); }static void setup (void ) { -tst_sig (NOFORK ,DEF_HANDLER , cleanup); - -tst_require_root (); - -TEST_PAUSE ; - -tst_tmpdir (); - - fildes =SAFE_OPEN (cleanup,TESTFILE ,O_RDWR |O_CREAT ,FILE_MODE ); + fd =SAFE_OPEN (TESTFILE ,O_RDWR |O_CREAT ,FILE_MODE ); }static void cleanup (void ) { -if (fildes >0 &&close (fildes)) -tst_resm (TWARN |TERRNO ,"close(%s) Failed" ,TESTFILE ); - -tst_rmdir (); +SAFE_CLOSE (fd); } + +static struct tst_test test = { + .tcnt =ARRAY_SIZE (tc), + .needs_root =1 , + .needs_tmpdir =1 , + .setup = setup, + .cleanup = cleanup, + .test = run, +};
--
2.17.1
PATCH 2/2 syscalls/fchown: Convert fchown05 to the new API
Hi!
testcases/kernel/syscalls/fchown/fchown05.c line 33
Quoted 11 lines
testcases/kernel/syscalls/fchown/fchown05.c +4
-6- {"Change Owner/Group ids", 700, 701}, - {"Change Owner id only", 702, -1}, - {"Change Owner id only", 703, 701}, - {"Change Group id only", -1, 704}, - {"Change Group id only", 703, 705}, - {NULL, 0, 0} + {700, 701}, + {702, 701}, + {702, 703}, + {704, 705} };
Can we please keep the tests where we check that -1 does not change the value and where we asert that the previously set value is still there after the fchown call?
We may as well add a check that fchown(fd, -1, -1) is no-op.
testcases/kernel/syscalls/fchown/fchown05.c line 39
Quoted 1 line
testcases/kernel/syscalls/fchown/fchown05.c +1
-0+TST_EXP_PASS (FCHOWN (fd, tc[i].uid, tc[i].gid));
This produces a bit ugly output, what about adding format string and parameters like:
TST_EXP_PASS(FCHOWN(fd, tc[i].uid, tc[i].gid), "fchwon(%i, %i, %i)", fd, tc[i].uid, tc[i].gid);
I guess I will push a similar patch that fixes this for fchown04 as well.
testcases/kernel/syscalls/fchown/fchown05.c line 55
Quoted 7 lines
static void cleanup(void)
{testcases/kernel/syscalls/fchown/fchown05.c +1
-4-if (fildes >0 &&close (fildes)) -tst_resm (TWARN |TERRNO ,"close(%s) Failed" ,TESTFILE ); - -tst_rmdir (); +SAFE_CLOSE (fd);
We really have to check if the fd has been opened, since the test can exit in any SAFE_MACRO() so this has to be:
if (fd > 0) SAFE_CLOSE(fd);
Other than these this is a nice cleanup.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 2/2 syscalls/fchown: Convert fchown05 to the new API
Hi, Cyril,
I just re-checked the latest code and submit the v2 version. Please see:
https://patchwork.ozlabs.org/project/ltp/patch/[email protected]/
Thanks for your review.
Kind Regards,
Ziyao