* [LTP] [PATCH 1/3] syscalls/setgroups01: Convert to new API
@ 2023-07-19 9:59 Yang Xu
2023-07-19 9:59 ` [LTP] [PATCH 2/3] syscalls/setgroups02: " Yang Xu
2023-07-19 9:59 ` [LTP] [PATCH 3/3] syscalls/getgroups03: " Yang Xu
0 siblings, 2 replies; 9+ messages in thread
From: Yang Xu @ 2023-07-19 9:59 UTC (permalink / raw)
To: ltp
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
testcases/kernel/syscalls/setgroups/setgroups01.c | 212 +++-------------------
1 file changed, 26 insertions(+), 186 deletions(-)
diff --git a/testcases/kernel/syscalls/setgroups/setgroups01.c b/testcases/kernel/syscalls/setgroups/setgroups01.c
index fed7f8e..0cb04b0 100644
--- a/testcases/kernel/syscalls/setgroups/setgroups01.c
+++ b/testcases/kernel/syscalls/setgroups/setgroups01.c
@@ -1,202 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
+ * Copyright (c) Linux Test Project, 2003-2023
*
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * Further, this software is distributed without any warranty that it is
- * free of the rightful claim of any third person regarding infringement
- * or the like. Any license provided herein, whether implied or
- * otherwise, applies only to this software file. Patent licenses, if
- * any, provided herein do not apply to combinations of this program with
- * other software, or any other product whatsoever.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
- * Mountain View, CA 94043, or:
- *
- * http://www.sgi.com
- *
- * For further information regarding this notice, see:
- *
- * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
- *
+ * Author: William Roske
+ * CO-PILOT: Dave Fenner
*/
-/* $Id: setgroups01.c,v 1.7 2009/11/02 13:57:18 subrata_modak Exp $ */
-/**********************************************************
- *
- * OS Test - Silicon Graphics, Inc.
- *
- * TEST IDENTIFIER : setgroups01
- *
- * EXECUTED BY : root
- *
- * TEST TITLE : Basic test for setgroups(2)
- *
- * PARENT DOCUMENT : usctpl01
- *
- * TEST CASE TOTAL : 1
- *
- * WALL CLOCK TIME : 1
- *
- * CPU TYPES : ALL
- *
- * AUTHOR : William Roske
- *
- * CO-PILOT : Dave Fenner
- *
- * DATE STARTED : 03/30/92
- *
- * INITIAL RELEASE : UNICOS 7.0
- *
- * TEST CASES
- *
- * 1.) setgroups(2) returns...(See Description)
- *
- * INPUT SPECIFICATIONS
- * The standard options for system call tests are accepted.
- * (See the parse_opts(3) man page).
- *
- * OUTPUT SPECIFICATIONS
- *$
- * DURATION
- * Terminates - with frequency and infinite modes.
- *
- * SIGNALS
- * Uses SIGUSR1 to pause before test if option set.
- * (See the parse_opts(3) man page).
- *
- * RESOURCES
- * None
- *
- * ENVIRONMENTAL NEEDS
- * No run-time environmental needs.
- *
- * SPECIAL PROCEDURAL REQUIREMENTS
- * None
- *
- * INTERCASE DEPENDENCIES
- * None
- *
- * DETAILED DESCRIPTION
- * This is a Phase I test for the setgroups(2) system call. It is intended
- * to provide a limited exposure of the system call, for now. It
- * should/will be extended when full functional tests are written for
- * setgroups(2).
- *
- * Setup:
- * Setup signal handling.
- * Pause for SIGUSR1 if option specified.
- *
- * Test:
- * 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.
- * Otherwise, Issue a PASS message.
- *
- * Cleanup:
- * Print errno log and/or timing stats if options given
- *
- *
- *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-#include <sys/param.h>
-#include <unistd.h>
-#include <grp.h>
-
-#include "test.h"
-
-#include "compat_16.h"
+/*\
+ * [Description]
+ *
+ * Check the basic functionality of the setgroups() system call.
+ */
-void setup();
-void cleanup();
+#include "tst_test.h"
+#include "compat_tst_16.h"
-TCID_DEFINE(setgroups01);
-int TST_TOTAL = 1;
+static int len = NGROUPS, ngrps;
-int len = NGROUPS, ngrps = 0;
-GID_T list[NGROUPS];
+static GID_T list[NGROUPS];
-int main(int ac, char **av)
+static void verify_setgroups(void)
{
- int lc;
-
- /***************************************************************
- * parse standard options
- ***************************************************************/
- tst_parse_opts(ac, av, NULL, NULL);
-
- /***************************************************************
- * perform global setup for test
- ***************************************************************/
- setup();
-
- /***************************************************************
- * check looping state if -c option given
- ***************************************************************/
- for (lc = 0; TEST_LOOPING(lc); lc++) {
-
- tst_count = 0;
-
- /*
- * Call setgroups(2)
- */
- TEST(SETGROUPS(cleanup, ngrps, list));
-
- /* check return code */
- if (TEST_RETURN == -1) {
- tst_resm(TFAIL,
- "setgroups(%d, list) Failed, errno=%d : %s",
- len, TEST_ERRNO, strerror(TEST_ERRNO));
- } else {
- tst_resm(TPASS,
- "setgroups(%d, list) returned %ld",
- len, TEST_RETURN);
- }
-
- }
-
- cleanup();
- tst_exit();
+ TST_EXP_POSITIVE(SETGROUPS(ngrps, list),
+ "setgroups(%d, list)", len);
}
-/***************************************************************
- * setup() - performs all ONE TIME setup for this test.
- ***************************************************************/
-void setup(void)
+static void setup(void)
{
-
- tst_require_root();
-
- tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
- TEST_PAUSE;
-
- ngrps = GETGROUPS(cleanup, len, list);
- if (ngrps == -1) {
- tst_brkm(TBROK, cleanup,
- "getgroups(%d, list) Failure. errno=%d : %s",
- len, errno, strerror(errno));
+ if (GETGROUPS(len, list) < 0) {
+ tst_brk(TBROK,
+ "getgroups(%d, list) Failure. errno=%d : %s",
+ len, errno, strerror(errno));
}
}
-/***************************************************************
- * cleanup() - performs all ONE TIME cleanup for this test at
- * completion or premature exit.
- ***************************************************************/
-void cleanup(void)
-{
-
-}
+static struct tst_test test = {
+ .test_all = verify_setgroups,
+ .setup = setup,
+ .needs_root = 1,
+};
--
1.8.3.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [LTP] [PATCH 2/3] syscalls/setgroups02: Convert to new API
2023-07-19 9:59 [LTP] [PATCH 1/3] syscalls/setgroups01: Convert to new API Yang Xu
@ 2023-07-19 9:59 ` Yang Xu
2023-07-26 12:51 ` Cyril Hrubis
2023-07-19 9:59 ` [LTP] [PATCH 3/3] syscalls/getgroups03: " Yang Xu
1 sibling, 1 reply; 9+ messages in thread
From: Yang Xu @ 2023-07-19 9:59 UTC (permalink / raw)
To: ltp
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
testcases/kernel/syscalls/setgroups/setgroups02.c | 194 +++++-----------------
1 file changed, 41 insertions(+), 153 deletions(-)
diff --git a/testcases/kernel/syscalls/setgroups/setgroups02.c b/testcases/kernel/syscalls/setgroups/setgroups02.c
index de23a4a..08bdc3c 100644
--- a/testcases/kernel/syscalls/setgroups/setgroups02.c
+++ b/testcases/kernel/syscalls/setgroups/setgroups02.c
@@ -1,180 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
- *
- * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Copyright (c) International Business Machines Corp., 2001
+ * Copyright (c) Linux Test Project, 2003-2023
+ * 07/2001 Ported by Wayne Boyer
*/
-/*
- * Test Name: setgroups02
- *
- * Test Description:
- * Verify that, only root process can invoke setgroups() system call to
- * set the supplementary group IDs of the process.
- *
- * Expected Result:
- * The call succeeds in setting all the supplementary group IDs of the
- * calling process. The new group should be set in the process supplemental
- * group list.
- *
- * Algorithm:
- * Setup:
- * Setup signal handling.
- * Pause for SIGUSR1 if option specified.
+/*\
+ * [Description]
*
- * Test:
- * 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.
- * Otherwise,
- * Verify the Functionality of system call
- * if successful,
- * Issue Functionality-Pass message.
- * Otherwise,
- * Issue Functionality-Fail message.
- * Cleanup:
- * Print errno log and/or timing stats if options given
+ * Check that only the root process can invoke setgroups()
+ * to set supplementary group IDs.
*
- * Usage: <for command-line>
- * setgroups02 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
- * where, -c n : Run n copies concurrently.
- * -f : Turn off functionality Testing.
- * -i n : Execute test n times.
- * -I x : Execute test for x seconds.
- * -P x : Pause for x seconds between iterations.
- * -t : Turn on syscall timing.
- *
- * HISTORY
- * 07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS:
- * This test should be run by 'super-user' (root) only.
+ * [Expected Result]
*
+ * The call succeeds in setting all the supplementary group IDs
+ * of the calling process. The new group should be set in the
+ * process supplemental group list.
*/
-#include <sys/types.h>
-#include <unistd.h>
-#include <errno.h>
-#include <pwd.h>
-#include <grp.h>
-#include <sys/param.h>
-#include "test.h"
+#include <pwd.h>
-#include "compat_16.h"
+#include "tst_test.h"
+#include "compat_tst_16.h"
#define TESTUSER "nobody"
-TCID_DEFINE(setgroups02);
-int TST_TOTAL = 1; /* Total number of test conditions */
-GID_T groups_list[NGROUPS]; /* Array to hold gids for getgroups() */
+static GID_T groups_get[NGROUPS];
+static GID_T groups_set[NGROUPS];
-struct passwd *user_info; /* struct. to hold test user info */
-void setup(); /* setup function for the test */
-void cleanup(); /* cleanup function for the test */
+static struct passwd *user_info;
-int main(int ac, char **av)
+static void verify_setgroups(void)
{
- int lc, i;
- int gidsetsize = 1; /* only one GID, the GID of TESTUSER */
- int PASS_FLAG = 0; /* used for checking group array */
+ int gidsetsize = 1;
- tst_parse_opts(ac, av, NULL, NULL);
+ TEST(SETGROUPS(gidsetsize, groups_set));
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
-
- tst_count = 0;
-
- /*
- * Call setgroups() to set supplimentary group IDs of
- * the calling super-user process to gid of TESTUSER.
- */
- TEST(SETGROUPS(cleanup, gidsetsize, groups_list));
+ if (TST_RET < 0) {
+ tst_res(TFAIL, "setgroups(%d, groups_set) Failed, "
+ "errno=%d : %s", gidsetsize,
+ TST_ERR, strerror(TST_ERR));
+ }
- if (TEST_RETURN == -1) {
- tst_resm(TFAIL, "setgroups(%d, groups_list) Failed, "
- "errno=%d : %s", gidsetsize, TEST_ERRNO,
- strerror(TEST_ERRNO));
- continue;
- }
+ GETGROUPS(gidsetsize, groups_get);
- /*
- * Call getgroups(2) to verify that
- * setgroups(2) successfully set the
- * supp. gids of TESTUSER.
- */
- groups_list[0] = '\0';
- if (GETGROUPS(cleanup, gidsetsize, groups_list) < 0) {
- tst_brkm(TFAIL, cleanup, "getgroups() Fails, "
- "error=%d", errno);
- }
- for (i = 0; i < NGROUPS; i++) {
- if (groups_list[i] == user_info->pw_gid) {
- tst_resm(TPASS,
- "Functionality of setgroups"
- "(%d, groups_list) successful",
- gidsetsize);
- PASS_FLAG = 1;
- }
- }
- if (PASS_FLAG == 0) {
- tst_resm(TFAIL, "Supplimentary gid %d not set "
- "for the process", user_info->pw_gid);
- }
+ if (groups_get[0] == groups_set[0]) {
+ tst_res(TPASS,
+ "Functionality of setgroups"
+ "(%d, groups_set) successful",
+ gidsetsize);
}
-
- cleanup();
- tst_exit();
}
-/*
- * setup() - performs all ONE TIME setup for this test.
- *
- * Make sure the test process uid is root.
- * Get the supplimentrary group id of test user from /etc/passwd file.
- */
-void setup(void)
+static void setup(void)
{
+ user_info = SAFE_GETPWNAM(TESTUSER);
- tst_require_root();
-
- tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
- TEST_PAUSE;
-
- /* Get the group id info. of TESTUSER from /etc/passwd */
- if ((user_info = getpwnam(TESTUSER)) == NULL) {
- tst_brkm(TFAIL, cleanup, "getpwnam(2) of %s Failed", TESTUSER);
- }
+ GID16_CHECK(user_info->pw_gid, setgroups);
- if (!GID_SIZE_CHECK(user_info->pw_gid)) {
- tst_brkm(TBROK,
- cleanup,
- "gid returned from getpwnam is too large for testing setgroups16");
- }
-
- groups_list[0] = user_info->pw_gid;
+ groups_set[0] = user_info->pw_gid;
}
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- * completion or premature exit.
- */
-void cleanup(void)
-{
-
-}
+static struct tst_test test = {
+ .test_all = verify_setgroups,
+ .setup = setup,
+ .needs_root = 1,
+};
--
1.8.3.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [LTP] [PATCH 3/3] syscalls/getgroups03: Convert to new API
2023-07-19 9:59 [LTP] [PATCH 1/3] syscalls/setgroups01: Convert to new API Yang Xu
2023-07-19 9:59 ` [LTP] [PATCH 2/3] syscalls/setgroups02: " Yang Xu
@ 2023-07-19 9:59 ` Yang Xu
2023-07-26 13:14 ` Cyril Hrubis
1 sibling, 1 reply; 9+ messages in thread
From: Yang Xu @ 2023-07-19 9:59 UTC (permalink / raw)
To: ltp
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=UTF-8, Size: 13462 bytes --]
1.merge setgroups04 to setgroups03
2.use safe_macro macro
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
runtest/syscalls | 2 -
testcases/kernel/syscalls/setgroups/.gitignore | 2 -
testcases/kernel/syscalls/setgroups/setgroups03.c | 251 ++++++----------------
testcases/kernel/syscalls/setgroups/setgroups04.c | 163 --------------
4 files changed, 61 insertions(+), 357 deletions(-)
delete mode 100644 testcases/kernel/syscalls/setgroups/setgroups04.c
diff --git a/runtest/syscalls b/runtest/syscalls
index b291511..150ddbd 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1325,8 +1325,6 @@ setgroups02 setgroups02
setgroups02_16 setgroups02_16
setgroups03 setgroups03
setgroups03_16 setgroups03_16
-setgroups04 setgroups04
-setgroups04_16 setgroups04_16
sethostname01 sethostname01
sethostname02 sethostname02
diff --git a/testcases/kernel/syscalls/setgroups/.gitignore b/testcases/kernel/syscalls/setgroups/.gitignore
index 0649a34..9de9282 100644
--- a/testcases/kernel/syscalls/setgroups/.gitignore
+++ b/testcases/kernel/syscalls/setgroups/.gitignore
@@ -4,5 +4,3 @@
/setgroups02_16
/setgroups03
/setgroups03_16
-/setgroups04
-/setgroups04_16
diff --git a/testcases/kernel/syscalls/setgroups/setgroups03.c b/testcases/kernel/syscalls/setgroups/setgroups03.c
index 490b069..c12d612 100644
--- a/testcases/kernel/syscalls/setgroups/setgroups03.c
+++ b/testcases/kernel/syscalls/setgroups/setgroups03.c
@@ -1,222 +1,93 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
- *
- * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Copyright (c) International Business Machines Corp., 2001
+ * Copyright (c) Linux Test Project, 2003-2023
+ * 07/2001 Ported by Wayne Boyer
+ * 05/2002 Ported by André Merlier
*/
-/*
- * Test Name: setgroups03
- *
- * Test Description:
- * Verify that,
- * 1. setgroups() fails with -1 and sets errno to EINVAL if the size
- * argument value is > NGROUPS
- * 2. setgroups() fails with -1 and sets errno to EPERM if the
- * calling process is not super-user.
- *
- * Expected Result:
- * setgroups() should fail with return value -1 and set expected errno.
- *
- * Algorithm:
- * Setup:
- * Setup signal handling.
- * Pause for SIGUSR1 if option specified.
+/*\
+ * [Description]
*
- * Test:
- * 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.
- * Otherwise,
- * Issue sys call returns unexpected value.
+ * Test for EINVAL, EPERM, EFAULT errors.
*
- * Cleanup:
- * Print errno log and/or timing stats if options given
+ * 1) setgroups() fails with EINVAL if the size argument value is > NGROUPS.
*
- * Usage: <for command-line>
- * setgroups03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- * where, -c n : Run n copies concurrently.
- * -f : Turn off functionality Testing.
- * -i n : Execute test n times.
- * -I x : Execute test for x seconds.
- * -P x : Pause for x seconds between iterations.
- * -t : Turn on syscall timing.
- *
- * HISTORY
- * 07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS:
- * This test should be executed by 'non-super-user' only.
+ * 2) setgroups() fails with EPERM if the calling process is not super-user.
*
+ * 3) setgroups() fails with EFAULT if the list has an invalid address.
*/
-#include <limits.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <errno.h>
-#include <pwd.h>
-#include <grp.h>
-#include "test.h"
+#include <pwd.h>
+#include <stdlib.h>
-#include "compat_16.h"
+#include "tst_test.h"
+#include "compat_tst_16.h"
#define TESTUSER "nobody"
-char nobody_uid[] = "nobody";
-struct passwd *ltpuser;
+static GID_T *glist1, *glist2, *glist3;
-TCID_DEFINE(setgroups03);
-int TST_TOTAL = 2;
+static struct tcase {
+ int gsize;
+ GID_T **glist;
+ int exp_errno;
+} tcases[] = {
+ {NGROUPS + 1, &glist1, EINVAL},
+ {NGROUPS, &glist2, EPERM},
+ {NGROUPS, &glist3, EFAULT},
+};
-GID_T *groups_list; /* Array to hold gids for getgroups() */
+static void change_uid(void)
+{
+ struct passwd *ltpuser, *user_info;
-int setup1(); /* setup function to test error EPERM */
-void setup(); /* setup function for the test */
-void cleanup(); /* cleanup function for the test */
+ ltpuser = SAFE_GETPWNAM(TESTUSER);
+ SAFE_SETEUID(ltpuser->pw_uid);
-struct test_case_t { /* test case struct. to hold ref. test cond's */
- size_t gsize_add;
- int list;
- char *desc;
- int exp_errno;
- int (*setupfunc) ();
-} Test_cases[] = {
- {
- 1, 1, "Size is > sysconf(_SC_NGROUPS_MAX)", EINVAL, NULL}, {
- 0, 2, "Permission denied, not super-user", EPERM, setup1}
-};
+ user_info = SAFE_GETPWNAM(TESTUSER);
+ GID16_CHECK(user_info->pw_gid, getpwnam);
+ glist2 = (GID_T *)&(user_info->pw_gid);
+}
-int main(int ac, char **av)
+static void restore_uid(void)
{
- int lc;
- int gidsetsize; /* total no. of groups */
- int i;
- char *test_desc; /* test specific error message */
- int ngroups_max = sysconf(_SC_NGROUPS_MAX); /* max no. of groups in the current system */
-
- tst_parse_opts(ac, av, NULL, NULL);
-
- groups_list = malloc(ngroups_max * sizeof(GID_T));
- if (groups_list == NULL) {
- tst_brkm(TBROK, NULL, "malloc failed to alloc %zu errno "
- " %d ", ngroups_max * sizeof(GID_T), errno);
- }
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
-
- tst_count = 0;
-
- for (i = 0; i < TST_TOTAL; i++) {
- if (Test_cases[i].setupfunc != NULL) {
- Test_cases[i].setupfunc();
- }
-
- gidsetsize = ngroups_max + Test_cases[i].gsize_add;
- test_desc = Test_cases[i].desc;
-
- /*
- * Call setgroups() to test different test conditions
- * verify that it fails with -1 return value and
- * sets appropriate errno.
- */
- TEST(SETGROUPS(cleanup, gidsetsize, groups_list));
-
- if (TEST_RETURN != -1) {
- tst_resm(TFAIL, "setgroups(%d) returned %ld, "
- "expected -1, errno=%d", gidsetsize,
- TEST_RETURN, Test_cases[i].exp_errno);
- continue;
- }
-
- if (TEST_ERRNO == Test_cases[i].exp_errno) {
- tst_resm(TPASS,
- "setgroups(%d) fails, %s, errno=%d",
- gidsetsize, test_desc, TEST_ERRNO);
- } else {
- tst_resm(TFAIL, "setgroups(%d) fails, %s, "
- "errno=%d, expected errno=%d",
- gidsetsize, test_desc, TEST_ERRNO,
- Test_cases[i].exp_errno);
- }
- }
-
- }
-
- cleanup();
-
- tst_exit();
+ struct passwd *root_info;
+
+ root_info = SAFE_GETPWNAM("root");
+ SAFE_SETEUID(root_info->pw_uid);
}
-/*
- * setup() - performs all ONE TIME setup for this test.
- *
- * Call individual test specific setup functions.
- */
-void setup(void)
+static void verify_setgroups(unsigned int i)
{
- tst_require_root();
+ struct tcase *tc = &tcases[i];
- tst_sig(NOFORK, DEF_HANDLER, cleanup);
+ if (tc->exp_errno == EPERM)
+ change_uid();
- TEST_PAUSE;
+ TST_EXP_FAIL(SETGROUPS(tc->gsize, *tc->glist), tc->exp_errno,
+ "setgroups(%d, groups_list)", tc->gsize);
+ if (tc->exp_errno == EPERM)
+ restore_uid();
}
-/*
- * setup1 - Setup function to test setgroups() which returns -1
- * and sets errno to EPERM.
- *
- * Get the user info. from /etc/passwd file.
- * This function returns 0 on success.
- */
-int setup1(void)
+static void setup(void)
{
- struct passwd *user_info; /* struct. to hold test user info */
-
-/* Switch to nobody user for correct error code collection */
- ltpuser = getpwnam(nobody_uid);
- if (seteuid(ltpuser->pw_uid) == -1) {
- tst_resm(TINFO, "setreuid failed to "
- "to set the effective uid to %d", ltpuser->pw_uid);
- perror("setreuid");
- }
-
- if ((user_info = getpwnam(TESTUSER)) == NULL) {
- tst_brkm(TFAIL, cleanup, "getpwnam(2) of %s Failed", TESTUSER);
- }
-
- if (!GID_SIZE_CHECK(user_info->pw_gid)) {
- tst_brkm(TBROK,
- cleanup,
- "gid returned from getpwnam is too large for testing setgroups16");
- }
- groups_list[0] = user_info->pw_gid;
- return 0;
+ glist1 = SAFE_MALLOC(NGROUPS * sizeof(GID_T));
+ glist3 = sbrk(0);
}
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- * completion or premature exit.
- */
-void cleanup(void)
+static void cleanup(void)
{
-
+ if (glist1)
+ free(glist1);
}
+
+static struct tst_test test = {
+ .test = verify_setgroups,
+ .tcnt = ARRAY_SIZE(tcases),
+ .setup = setup,
+ .cleanup = cleanup,
+ .needs_root = 1,
+};
diff --git a/testcases/kernel/syscalls/setgroups/setgroups04.c b/testcases/kernel/syscalls/setgroups/setgroups04.c
deleted file mode 100644
index 971c86b..0000000
--- a/testcases/kernel/syscalls/setgroups/setgroups04.c
+++ /dev/null
@@ -1,163 +0,0 @@
-/*
- * Copyright (C) Bull S.A. 2001
- * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Name: setgroups04
- *
- * Test Description:
- * Verify that, setgroups() fails with -1 and sets errno to EFAULT if the list has an invalid address.
- *
- * Expected Result:
- * setgroups() should fail with return value -1 and set expected errno.
- *
- * Algorithm:
- * Setup:
- * Setup signal handling.
- * Pause for SIGUSR1 if option specified.
- *
- * Test:
- * 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.
- * Otherwise,
- * Issue sys call returns unexpected value.
- *
- * Cleanup:
- * Print errno log and/or timing stats if options given
- *
- * Usage: <for command-line>
- * setgroups04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- * where, -c n : Run n copies concurrently.
- * -f : Turn off functionality Testing.
- * -i n : Execute test n times.
- * -I x : Execute test for x seconds.
- * -P x : Pause for x seconds between iterations.
- * -t : Turn on syscall timing.
- *
- * HISTORY
- * 05/2002 Ported by André Merlier
- *
- * RESTRICTIONS:
- * none.
- *
- */
-#include <sys/types.h>
-#include <sys/param.h>
-#include <unistd.h>
-#include <errno.h>
-#include <pwd.h>
-#include <grp.h>
-
-#include "test.h"
-
-#include "compat_16.h"
-
-TCID_DEFINE(setgroups04);
-int TST_TOTAL = 1;
-
-GID_T groups_list[NGROUPS];
-
-void setup(); /* setup function for the test */
-void cleanup(); /* cleanup function for the test */
-
-#if !defined(UCLINUX)
-
-int main(int ac, char **av)
-{
- int lc;
- int gidsetsize; /* total no. of groups */
- char *test_desc; /* test specific error message */
-
- tst_parse_opts(ac, av, NULL, NULL);
-
- /* Perform setup for test */
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
-
- tst_count = 0;
-
- gidsetsize = NGROUPS;
- test_desc = "EFAULT";
-
- /*
- * Call setgroups() to test condition
- * verify that it fails with -1 return value and
- * sets appropriate errno.
- */
- TEST(SETGROUPS(cleanup, gidsetsize, sbrk(0)));
-
- if (TEST_RETURN != -1) {
- tst_resm(TFAIL, "setgroups() returned %ld, "
- "expected -1, errno=%d", TEST_RETURN,
- EFAULT);
- } else {
-
- if (TEST_ERRNO == EFAULT) {
- tst_resm(TPASS,
- "setgroups() fails with expected "
- "error EFAULT errno:%d", TEST_ERRNO);
- } else {
- tst_resm(TFAIL, "setgroups() fails, %s, "
- "errno=%d, expected errno=%d",
- test_desc, TEST_ERRNO, EFAULT);
- }
- }
-
- }
-
- cleanup();
- tst_exit();
-
-}
-
-#else
-
-int main(void)
-{
- tst_resm(TINFO, "test is not available on uClinux");
- tst_exit();
-}
-
-#endif /* if !defined(UCLINUX) */
-
-/*
- * setup()
- */
-void setup(void)
-{
- tst_require_root();
-
- tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
- TEST_PAUSE;
-
-}
-
-/*
- * cleanup()
- */
-void cleanup(void)
-{
-
-}
--
1.8.3.1
[-- Attachment #2: Type: text/plain, Size: 60 bytes --]
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [LTP] [PATCH 2/3] syscalls/setgroups02: Convert to new API
2023-07-19 9:59 ` [LTP] [PATCH 2/3] syscalls/setgroups02: " Yang Xu
@ 2023-07-26 12:51 ` Cyril Hrubis
2023-07-26 13:17 ` Cyril Hrubis
2023-08-01 11:04 ` Yang Xu (Fujitsu)
0 siblings, 2 replies; 9+ messages in thread
From: Cyril Hrubis @ 2023-07-26 12:51 UTC (permalink / raw)
To: Yang Xu; +Cc: ltp
Hi!
> + TEST(SETGROUPS(gidsetsize, groups_set));
TST_EXP_PASS(SETGROUPS(gidsetsize, groups_set)) ?
> - setup();
> -
> - for (lc = 0; TEST_LOOPING(lc); lc++) {
> -
> - tst_count = 0;
> -
> - /*
> - * Call setgroups() to set supplimentary group IDs of
> - * the calling super-user process to gid of TESTUSER.
> - */
> - TEST(SETGROUPS(cleanup, gidsetsize, groups_list));
> + if (TST_RET < 0) {
> + tst_res(TFAIL, "setgroups(%d, groups_set) Failed, "
> + "errno=%d : %s", gidsetsize,
> + TST_ERR, strerror(TST_ERR));
> + }
>
> - if (TEST_RETURN == -1) {
> - tst_resm(TFAIL, "setgroups(%d, groups_list) Failed, "
> - "errno=%d : %s", gidsetsize, TEST_ERRNO,
> - strerror(TEST_ERRNO));
> - continue;
> - }
> + GETGROUPS(gidsetsize, groups_get);
TST_EXP_VAL(GETGROUPS(gitsetsize, groups_get), 1) ?
Also I suppose that we should make sure that the groups_get[0] value is
not equal to the groups_set[0], expecially with -i 2
So maybe:
groups_get[0] = ~groups_set[0];
> - /*
> - * Call getgroups(2) to verify that
> - * setgroups(2) successfully set the
> - * supp. gids of TESTUSER.
> - */
> - groups_list[0] = '\0';
> - if (GETGROUPS(cleanup, gidsetsize, groups_list) < 0) {
> - tst_brkm(TFAIL, cleanup, "getgroups() Fails, "
> - "error=%d", errno);
> - }
> - for (i = 0; i < NGROUPS; i++) {
> - if (groups_list[i] == user_info->pw_gid) {
> - tst_resm(TPASS,
> - "Functionality of setgroups"
> - "(%d, groups_list) successful",
> - gidsetsize);
> - PASS_FLAG = 1;
> - }
> - }
> - if (PASS_FLAG == 0) {
> - tst_resm(TFAIL, "Supplimentary gid %d not set "
> - "for the process", user_info->pw_gid);
> - }
> + if (groups_get[0] == groups_set[0]) {
> + tst_res(TPASS,
> + "Functionality of setgroups"
> + "(%d, groups_set) successful",
> + gidsetsize);
> }
TST_EXP_EQ_LI(groups_get[0], groups_set[0]);
At least that will produce TFAIL results if they don't match, which is
missing here.
> - cleanup();
> - tst_exit();
> }
>
> -/*
> - * setup() - performs all ONE TIME setup for this test.
> - *
> - * Make sure the test process uid is root.
> - * Get the supplimentrary group id of test user from /etc/passwd file.
> - */
> -void setup(void)
> +static void setup(void)
> {
> + user_info = SAFE_GETPWNAM(TESTUSER);
>
> - tst_require_root();
> -
> - tst_sig(NOFORK, DEF_HANDLER, cleanup);
> -
> - TEST_PAUSE;
> -
> - /* Get the group id info. of TESTUSER from /etc/passwd */
> - if ((user_info = getpwnam(TESTUSER)) == NULL) {
> - tst_brkm(TFAIL, cleanup, "getpwnam(2) of %s Failed", TESTUSER);
> - }
> + GID16_CHECK(user_info->pw_gid, setgroups);
>
> - if (!GID_SIZE_CHECK(user_info->pw_gid)) {
> - tst_brkm(TBROK,
> - cleanup,
> - "gid returned from getpwnam is too large for testing setgroups16");
> - }
> -
> - groups_list[0] = user_info->pw_gid;
> + groups_set[0] = user_info->pw_gid;
Do we really need this? I suppose that any random value will do, e.g.
groups_set[0] = 42;
is going to be as good as anything else.
> }
>
> -/*
> - * cleanup() - performs all ONE TIME cleanup for this test at
> - * completion or premature exit.
> - */
> -void cleanup(void)
> -{
> -
> -}
> +static struct tst_test test = {
> + .test_all = verify_setgroups,
> + .setup = setup,
> + .needs_root = 1,
> +};
> --
> 1.8.3.1
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [LTP] [PATCH 3/3] syscalls/getgroups03: Convert to new API
2023-07-19 9:59 ` [LTP] [PATCH 3/3] syscalls/getgroups03: " Yang Xu
@ 2023-07-26 13:14 ` Cyril Hrubis
2023-08-01 10:53 ` Yang Xu (Fujitsu)
0 siblings, 1 reply; 9+ messages in thread
From: Cyril Hrubis @ 2023-07-26 13:14 UTC (permalink / raw)
To: Yang Xu; +Cc: ltp
Hi!
There is a typo in the subject which got me confused for a while
s/getgroups/setgroups/
> 1.merge setgroups04 to setgroups03
> 2.use safe_macro macro
>
> Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
> ---
> runtest/syscalls | 2 -
> testcases/kernel/syscalls/setgroups/.gitignore | 2 -
> testcases/kernel/syscalls/setgroups/setgroups03.c | 251 ++++++----------------
> testcases/kernel/syscalls/setgroups/setgroups04.c | 163 --------------
> 4 files changed, 61 insertions(+), 357 deletions(-)
> delete mode 100644 testcases/kernel/syscalls/setgroups/setgroups04.c
>
> diff --git a/runtest/syscalls b/runtest/syscalls
> index b291511..150ddbd 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -1325,8 +1325,6 @@ setgroups02 setgroups02
> setgroups02_16 setgroups02_16
> setgroups03 setgroups03
> setgroups03_16 setgroups03_16
> -setgroups04 setgroups04
> -setgroups04_16 setgroups04_16
>
> sethostname01 sethostname01
> sethostname02 sethostname02
> diff --git a/testcases/kernel/syscalls/setgroups/.gitignore b/testcases/kernel/syscalls/setgroups/.gitignore
> index 0649a34..9de9282 100644
> --- a/testcases/kernel/syscalls/setgroups/.gitignore
> +++ b/testcases/kernel/syscalls/setgroups/.gitignore
> @@ -4,5 +4,3 @@
> /setgroups02_16
> /setgroups03
> /setgroups03_16
> -/setgroups04
> -/setgroups04_16
> diff --git a/testcases/kernel/syscalls/setgroups/setgroups03.c b/testcases/kernel/syscalls/setgroups/setgroups03.c
> index 490b069..c12d612 100644
> --- a/testcases/kernel/syscalls/setgroups/setgroups03.c
> +++ b/testcases/kernel/syscalls/setgroups/setgroups03.c
> @@ -1,222 +1,93 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> /*
> - *
> - * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + * Copyright (c) International Business Machines Corp., 2001
> + * Copyright (c) Linux Test Project, 2003-2023
> + * 07/2001 Ported by Wayne Boyer
> + * 05/2002 Ported by André Merlier
> */
>
> -/*
> - * Test Name: setgroups03
> - *
> - * Test Description:
> - * Verify that,
> - * 1. setgroups() fails with -1 and sets errno to EINVAL if the size
> - * argument value is > NGROUPS
> - * 2. setgroups() fails with -1 and sets errno to EPERM if the
> - * calling process is not super-user.
> - *
> - * Expected Result:
> - * setgroups() should fail with return value -1 and set expected errno.
> - *
> - * Algorithm:
> - * Setup:
> - * Setup signal handling.
> - * Pause for SIGUSR1 if option specified.
> +/*\
> + * [Description]
> *
> - * Test:
> - * 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.
> - * Otherwise,
> - * Issue sys call returns unexpected value.
> + * Test for EINVAL, EPERM, EFAULT errors.
> *
> - * Cleanup:
> - * Print errno log and/or timing stats if options given
> + * 1) setgroups() fails with EINVAL if the size argument value is > NGROUPS.
> *
> - * Usage: <for command-line>
> - * setgroups03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
> - * where, -c n : Run n copies concurrently.
> - * -f : Turn off functionality Testing.
> - * -i n : Execute test n times.
> - * -I x : Execute test for x seconds.
> - * -P x : Pause for x seconds between iterations.
> - * -t : Turn on syscall timing.
> - *
> - * HISTORY
> - * 07/2001 Ported by Wayne Boyer
> - *
> - * RESTRICTIONS:
> - * This test should be executed by 'non-super-user' only.
> + * 2) setgroups() fails with EPERM if the calling process is not super-user.
> *
> + * 3) setgroups() fails with EFAULT if the list has an invalid address.
> */
> -#include <limits.h>
> -#include <sys/types.h>
> -#include <unistd.h>
> -#include <errno.h>
> -#include <pwd.h>
> -#include <grp.h>
>
> -#include "test.h"
> +#include <pwd.h>
> +#include <stdlib.h>
>
> -#include "compat_16.h"
> +#include "tst_test.h"
> +#include "compat_tst_16.h"
>
> #define TESTUSER "nobody"
>
> -char nobody_uid[] = "nobody";
> -struct passwd *ltpuser;
> +static GID_T *glist1, *glist2, *glist3;
>
> -TCID_DEFINE(setgroups03);
> -int TST_TOTAL = 2;
> +static struct tcase {
> + int gsize;
> + GID_T **glist;
> + int exp_errno;
> +} tcases[] = {
> + {NGROUPS + 1, &glist1, EINVAL},
> + {NGROUPS, &glist2, EPERM},
> + {NGROUPS, &glist3, EFAULT},
> +};
>
> -GID_T *groups_list; /* Array to hold gids for getgroups() */
> +static void change_uid(void)
> +{
> + struct passwd *ltpuser, *user_info;
>
> -int setup1(); /* setup function to test error EPERM */
> -void setup(); /* setup function for the test */
> -void cleanup(); /* cleanup function for the test */
> + ltpuser = SAFE_GETPWNAM(TESTUSER);
> + SAFE_SETEUID(ltpuser->pw_uid);
>
> -struct test_case_t { /* test case struct. to hold ref. test cond's */
> - size_t gsize_add;
> - int list;
> - char *desc;
> - int exp_errno;
> - int (*setupfunc) ();
> -} Test_cases[] = {
> - {
> - 1, 1, "Size is > sysconf(_SC_NGROUPS_MAX)", EINVAL, NULL}, {
> - 0, 2, "Permission denied, not super-user", EPERM, setup1}
> -};
> + user_info = SAFE_GETPWNAM(TESTUSER);
We usually get the user entry once in the test setup and save it for
later use...
> + GID16_CHECK(user_info->pw_gid, getpwnam);
> + glist2 = (GID_T *)&(user_info->pw_gid);
> +}
>
> -int main(int ac, char **av)
> +static void restore_uid(void)
> {
> - int lc;
> - int gidsetsize; /* total no. of groups */
> - int i;
> - char *test_desc; /* test specific error message */
> - int ngroups_max = sysconf(_SC_NGROUPS_MAX); /* max no. of groups in the current system */
> -
> - tst_parse_opts(ac, av, NULL, NULL);
> -
> - groups_list = malloc(ngroups_max * sizeof(GID_T));
> - if (groups_list == NULL) {
> - tst_brkm(TBROK, NULL, "malloc failed to alloc %zu errno "
> - " %d ", ngroups_max * sizeof(GID_T), errno);
> - }
> -
> - setup();
> -
> - for (lc = 0; TEST_LOOPING(lc); lc++) {
> -
> - tst_count = 0;
> -
> - for (i = 0; i < TST_TOTAL; i++) {
> - if (Test_cases[i].setupfunc != NULL) {
> - Test_cases[i].setupfunc();
> - }
> -
> - gidsetsize = ngroups_max + Test_cases[i].gsize_add;
> - test_desc = Test_cases[i].desc;
> -
> - /*
> - * Call setgroups() to test different test conditions
> - * verify that it fails with -1 return value and
> - * sets appropriate errno.
> - */
> - TEST(SETGROUPS(cleanup, gidsetsize, groups_list));
> -
> - if (TEST_RETURN != -1) {
> - tst_resm(TFAIL, "setgroups(%d) returned %ld, "
> - "expected -1, errno=%d", gidsetsize,
> - TEST_RETURN, Test_cases[i].exp_errno);
> - continue;
> - }
> -
> - if (TEST_ERRNO == Test_cases[i].exp_errno) {
> - tst_resm(TPASS,
> - "setgroups(%d) fails, %s, errno=%d",
> - gidsetsize, test_desc, TEST_ERRNO);
> - } else {
> - tst_resm(TFAIL, "setgroups(%d) fails, %s, "
> - "errno=%d, expected errno=%d",
> - gidsetsize, test_desc, TEST_ERRNO,
> - Test_cases[i].exp_errno);
> - }
> - }
> -
> - }
> -
> - cleanup();
> -
> - tst_exit();
> + struct passwd *root_info;
> +
> + root_info = SAFE_GETPWNAM("root");
> + SAFE_SETEUID(root_info->pw_uid);
> }
>
> -/*
> - * setup() - performs all ONE TIME setup for this test.
> - *
> - * Call individual test specific setup functions.
> - */
> -void setup(void)
> +static void verify_setgroups(unsigned int i)
> {
> - tst_require_root();
> + struct tcase *tc = &tcases[i];
>
> - tst_sig(NOFORK, DEF_HANDLER, cleanup);
> + if (tc->exp_errno == EPERM)
> + change_uid();
>
> - TEST_PAUSE;
> + TST_EXP_FAIL(SETGROUPS(tc->gsize, *tc->glist), tc->exp_errno,
> + "setgroups(%d, groups_list)", tc->gsize);
>
> + if (tc->exp_errno == EPERM)
> + restore_uid();
> }
>
> -/*
> - * setup1 - Setup function to test setgroups() which returns -1
> - * and sets errno to EPERM.
> - *
> - * Get the user info. from /etc/passwd file.
> - * This function returns 0 on success.
> - */
> -int setup1(void)
> +static void setup(void)
> {
> - struct passwd *user_info; /* struct. to hold test user info */
> -
> -/* Switch to nobody user for correct error code collection */
> - ltpuser = getpwnam(nobody_uid);
> - if (seteuid(ltpuser->pw_uid) == -1) {
> - tst_resm(TINFO, "setreuid failed to "
> - "to set the effective uid to %d", ltpuser->pw_uid);
> - perror("setreuid");
> - }
> -
> - if ((user_info = getpwnam(TESTUSER)) == NULL) {
> - tst_brkm(TFAIL, cleanup, "getpwnam(2) of %s Failed", TESTUSER);
> - }
> -
> - if (!GID_SIZE_CHECK(user_info->pw_gid)) {
> - tst_brkm(TBROK,
> - cleanup,
> - "gid returned from getpwnam is too large for testing setgroups16");
> - }
> - groups_list[0] = user_info->pw_gid;
> - return 0;
> + glist1 = SAFE_MALLOC(NGROUPS * sizeof(GID_T));
I guess that technically this should be (NGROUPS+1) * sizeof(GID_T) even
though in reality kernel will return error even before it attempts to
access the buffer.
> + glist3 = sbrk(0);
tst_get_bad_addr() please.
Otherwise it looks good.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [LTP] [PATCH 2/3] syscalls/setgroups02: Convert to new API
2023-07-26 12:51 ` Cyril Hrubis
@ 2023-07-26 13:17 ` Cyril Hrubis
2023-08-01 10:52 ` Yang Xu (Fujitsu)
2023-08-01 11:04 ` Yang Xu (Fujitsu)
1 sibling, 1 reply; 9+ messages in thread
From: Cyril Hrubis @ 2023-07-26 13:17 UTC (permalink / raw)
To: Yang Xu; +Cc: ltp
Hi!
Also can we please allocate the buffers passed to setgroups/getgroups as guarded buffers[1] of size 1?
[1] https://github.com/linux-test-project/ltp/wiki/C-Test-API#131-guarded-buffers
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [LTP] [PATCH 2/3] syscalls/setgroups02: Convert to new API
2023-07-26 13:17 ` Cyril Hrubis
@ 2023-08-01 10:52 ` Yang Xu (Fujitsu)
0 siblings, 0 replies; 9+ messages in thread
From: Yang Xu (Fujitsu) @ 2023-08-01 10:52 UTC (permalink / raw)
To: Cyril Hrubis; +Cc: ltp@lists.linux.it
Hi Cyril
> Hi!
> Also can we please allocate the buffers passed to setgroups/getgroups as guarded buffers[1] of size 1?
>
> [1] https://github.com/linux-test-project/ltp/wiki/C-Test-API#131-guarded-buffers
Yes,I will update it as you suggested.
Best Regards
Yang Xu
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [LTP] [PATCH 3/3] syscalls/getgroups03: Convert to new API
2023-07-26 13:14 ` Cyril Hrubis
@ 2023-08-01 10:53 ` Yang Xu (Fujitsu)
0 siblings, 0 replies; 9+ messages in thread
From: Yang Xu (Fujitsu) @ 2023-08-01 10:53 UTC (permalink / raw)
To: Cyril Hrubis; +Cc: ltp@lists.linux.it
Hi Cyril
Hi!
There is a typo in the subject which got me confused for a while
s/getgroups/setgroups/
Sorry for my mistake
1.merge setgroups04 to setgroups03
2.use safe_macro macro
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com><mailto:xuyang2018.jy@fujitsu.com>
---
runtest/syscalls | 2 -
testcases/kernel/syscalls/setgroups/.gitignore | 2 -
testcases/kernel/syscalls/setgroups/setgroups03.c | 251 ++++++----------------
testcases/kernel/syscalls/setgroups/setgroups04.c | 163 --------------
4 files changed, 61 insertions(+), 357 deletions(-)
delete mode 100644 testcases/kernel/syscalls/setgroups/setgroups04.c
diff --git a/runtest/syscalls b/runtest/syscalls
index b291511..150ddbd 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1325,8 +1325,6 @@ setgroups02 setgroups02
setgroups02_16 setgroups02_16
setgroups03 setgroups03
setgroups03_16 setgroups03_16
-setgroups04 setgroups04
-setgroups04_16 setgroups04_16
sethostname01 sethostname01
sethostname02 sethostname02
diff --git a/testcases/kernel/syscalls/setgroups/.gitignore b/testcases/kernel/syscalls/setgroups/.gitignore
index 0649a34..9de9282 100644
--- a/testcases/kernel/syscalls/setgroups/.gitignore
+++ b/testcases/kernel/syscalls/setgroups/.gitignore
@@ -4,5 +4,3 @@
/setgroups02_16
/setgroups03
/setgroups03_16
-/setgroups04
-/setgroups04_16
diff --git a/testcases/kernel/syscalls/setgroups/setgroups03.c b/testcases/kernel/syscalls/setgroups/setgroups03.c
index 490b069..c12d612 100644
--- a/testcases/kernel/syscalls/setgroups/setgroups03.c
+++ b/testcases/kernel/syscalls/setgroups/setgroups03.c
@@ -1,222 +1,93 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
- *
- * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Copyright (c) International Business Machines Corp., 2001
+ * Copyright (c) Linux Test Project, 2003-2023
+ * 07/2001 Ported by Wayne Boyer
+ * 05/2002 Ported by André Merlier
*/
-/*
- * Test Name: setgroups03
- *
- * Test Description:
- * Verify that,
- * 1. setgroups() fails with -1 and sets errno to EINVAL if the size
- * argument value is > NGROUPS
- * 2. setgroups() fails with -1 and sets errno to EPERM if the
- * calling process is not super-user.
- *
- * Expected Result:
- * setgroups() should fail with return value -1 and set expected errno.
- *
- * Algorithm:
- * Setup:
- * Setup signal handling.
- * Pause for SIGUSR1 if option specified.
+/*\
+ * [Description]
*
- * Test:
- * 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.
- * Otherwise,
- * Issue sys call returns unexpected value.
+ * Test for EINVAL, EPERM, EFAULT errors.
*
- * Cleanup:
- * Print errno log and/or timing stats if options given
+ * 1) setgroups() fails with EINVAL if the size argument value is > NGROUPS.
*
- * Usage: <for command-line>
- * setgroups03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- * where, -c n : Run n copies concurrently.
- * -f : Turn off functionality Testing.
- * -i n : Execute test n times.
- * -I x : Execute test for x seconds.
- * -P x : Pause for x seconds between iterations.
- * -t : Turn on syscall timing.
- *
- * HISTORY
- * 07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS:
- * This test should be executed by 'non-super-user' only.
+ * 2) setgroups() fails with EPERM if the calling process is not super-user.
*
+ * 3) setgroups() fails with EFAULT if the list has an invalid address.
*/
-#include <limits.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <errno.h>
-#include <pwd.h>
-#include <grp.h>
-#include "test.h"
+#include <pwd.h>
+#include <stdlib.h>
-#include "compat_16.h"
+#include "tst_test.h"
+#include "compat_tst_16.h"
#define TESTUSER "nobody"
-char nobody_uid[] = "nobody";
-struct passwd *ltpuser;
+static GID_T *glist1, *glist2, *glist3;
-TCID_DEFINE(setgroups03);
-int TST_TOTAL = 2;
+static struct tcase {
+ int gsize;
+ GID_T **glist;
+ int exp_errno;
+} tcases[] = {
+ {NGROUPS + 1, &glist1, EINVAL},
+ {NGROUPS, &glist2, EPERM},
+ {NGROUPS, &glist3, EFAULT},
+};
-GID_T *groups_list; /* Array to hold gids for getgroups() */
+static void change_uid(void)
+{
+ struct passwd *ltpuser, *user_info;
-int setup1(); /* setup function to test error EPERM */
-void setup(); /* setup function for the test */
-void cleanup(); /* cleanup function for the test */
+ ltpuser = SAFE_GETPWNAM(TESTUSER);
+ SAFE_SETEUID(ltpuser->pw_uid);
-struct test_case_t { /* test case struct. to hold ref. test cond's */
- size_t gsize_add;
- int list;
- char *desc;
- int exp_errno;
- int (*setupfunc) ();
-} Test_cases[] = {
- {
- 1, 1, "Size is > sysconf(_SC_NGROUPS_MAX)", EINVAL, NULL}, {
- 0, 2, "Permission denied, not super-user", EPERM, setup1}
-};
+ user_info = SAFE_GETPWNAM(TESTUSER);
We usually get the user entry once in the test setup and save it for
later use...
OK.
+ GID16_CHECK(user_info->pw_gid, getpwnam);
+ glist2 = (GID_T *)&(user_info->pw_gid);
+}
-int main(int ac, char **av)
+static void restore_uid(void)
{
- int lc;
- int gidsetsize; /* total no. of groups */
- int i;
- char *test_desc; /* test specific error message */
- int ngroups_max = sysconf(_SC_NGROUPS_MAX); /* max no. of groups in the current system */
-
- tst_parse_opts(ac, av, NULL, NULL);
-
- groups_list = malloc(ngroups_max * sizeof(GID_T));
- if (groups_list == NULL) {
- tst_brkm(TBROK, NULL, "malloc failed to alloc %zu errno "
- " %d ", ngroups_max * sizeof(GID_T), errno);
- }
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
-
- tst_count = 0;
-
- for (i = 0; i < TST_TOTAL; i++) {
- if (Test_cases[i].setupfunc != NULL) {
- Test_cases[i].setupfunc();
- }
-
- gidsetsize = ngroups_max + Test_cases[i].gsize_add;
- test_desc = Test_cases[i].desc;
-
- /*
- * Call setgroups() to test different test conditions
- * verify that it fails with -1 return value and
- * sets appropriate errno.
- */
- TEST(SETGROUPS(cleanup, gidsetsize, groups_list));
-
- if (TEST_RETURN != -1) {
- tst_resm(TFAIL, "setgroups(%d) returned %ld, "
- "expected -1, errno=%d", gidsetsize,
- TEST_RETURN, Test_cases[i].exp_errno);
- continue;
- }
-
- if (TEST_ERRNO == Test_cases[i].exp_errno) {
- tst_resm(TPASS,
- "setgroups(%d) fails, %s, errno=%d",
- gidsetsize, test_desc, TEST_ERRNO);
- } else {
- tst_resm(TFAIL, "setgroups(%d) fails, %s, "
- "errno=%d, expected errno=%d",
- gidsetsize, test_desc, TEST_ERRNO,
- Test_cases[i].exp_errno);
- }
- }
-
- }
-
- cleanup();
-
- tst_exit();
+ struct passwd *root_info;
+
+ root_info = SAFE_GETPWNAM("root");
+ SAFE_SETEUID(root_info->pw_uid);
}
-/*
- * setup() - performs all ONE TIME setup for this test.
- *
- * Call individual test specific setup functions.
- */
-void setup(void)
+static void verify_setgroups(unsigned int i)
{
- tst_require_root();
+ struct tcase *tc = &tcases[i];
- tst_sig(NOFORK, DEF_HANDLER, cleanup);
+ if (tc->exp_errno == EPERM)
+ change_uid();
- TEST_PAUSE;
+ TST_EXP_FAIL(SETGROUPS(tc->gsize, *tc->glist), tc->exp_errno,
+ "setgroups(%d, groups_list)", tc->gsize);
+ if (tc->exp_errno == EPERM)
+ restore_uid();
}
-/*
- * setup1 - Setup function to test setgroups() which returns -1
- * and sets errno to EPERM.
- *
- * Get the user info. from /etc/passwd file.
- * This function returns 0 on success.
- */
-int setup1(void)
+static void setup(void)
{
- struct passwd *user_info; /* struct. to hold test user info */
-
-/* Switch to nobody user for correct error code collection */
- ltpuser = getpwnam(nobody_uid);
- if (seteuid(ltpuser->pw_uid) == -1) {
- tst_resm(TINFO, "setreuid failed to "
- "to set the effective uid to %d", ltpuser->pw_uid);
- perror("setreuid");
- }
-
- if ((user_info = getpwnam(TESTUSER)) == NULL) {
- tst_brkm(TFAIL, cleanup, "getpwnam(2) of %s Failed", TESTUSER);
- }
-
- if (!GID_SIZE_CHECK(user_info->pw_gid)) {
- tst_brkm(TBROK,
- cleanup,
- "gid returned from getpwnam is too large for testing setgroups16");
- }
- groups_list[0] = user_info->pw_gid;
- return 0;
+ glist1 = SAFE_MALLOC(NGROUPS * sizeof(GID_T));
I guess that technically this should be (NGROUPS+1) * sizeof(GID_T) even
though in reality kernel will return error even before it attempts to
access the buffer.
Agree.
+ glist3 = sbrk(0);
tst_get_bad_addr() please.
Otherwise it looks good.
I will update it.
Best Regards
Yang Xu
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [LTP] [PATCH 2/3] syscalls/setgroups02: Convert to new API
2023-07-26 12:51 ` Cyril Hrubis
2023-07-26 13:17 ` Cyril Hrubis
@ 2023-08-01 11:04 ` Yang Xu (Fujitsu)
1 sibling, 0 replies; 9+ messages in thread
From: Yang Xu (Fujitsu) @ 2023-08-01 11:04 UTC (permalink / raw)
To: Cyril Hrubis; +Cc: ltp@lists.linux.it
Hi Cyril
Hi!
+ TEST(SETGROUPS(gidsetsize, groups_set));
TST_EXP_PASS(SETGROUPS(gidsetsize, groups_set)) ?
OK.
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
-
- tst_count = 0;
-
- /*
- * Call setgroups() to set supplimentary group IDs of
- * the calling super-user process to gid of TESTUSER.
- */
- TEST(SETGROUPS(cleanup, gidsetsize, groups_list));
+ if (TST_RET < 0) {
+ tst_res(TFAIL, "setgroups(%d, groups_set) Failed, "
+ "errno=%d : %s", gidsetsize,
+ TST_ERR, strerror(TST_ERR));
+ }
- if (TEST_RETURN == -1) {
- tst_resm(TFAIL, "setgroups(%d, groups_list) Failed, "
- "errno=%d : %s", gidsetsize, TEST_ERRNO,
- strerror(TEST_ERRNO));
- continue;
- }
+ GETGROUPS(gidsetsize, groups_get);
TST_EXP_VAL(GETGROUPS(gitsetsize, groups_get), 1) ?
Also I suppose that we should make sure that the groups_get[0] value is
not equal to the groups_set[0], expecially with -i 2
So maybe:
groups_get[0] = ~groups_set[0];
I will update it, but the results we assume are equal.
- /*
- * Call getgroups(2) to verify that
- * setgroups(2) successfully set the
- * supp. gids of TESTUSER.
- */
- groups_list[0] = '\0';
- if (GETGROUPS(cleanup, gidsetsize, groups_list) < 0) {
- tst_brkm(TFAIL, cleanup, "getgroups() Fails, "
- "error=%d", errno);
- }
- for (i = 0; i < NGROUPS; i++) {
- if (groups_list[i] == user_info->pw_gid) {
- tst_resm(TPASS,
- "Functionality of setgroups"
- "(%d, groups_list) successful",
- gidsetsize);
- PASS_FLAG = 1;
- }
- }
- if (PASS_FLAG == 0) {
- tst_resm(TFAIL, "Supplimentary gid %d not set "
- "for the process", user_info->pw_gid);
- }
+ if (groups_get[0] == groups_set[0]) {
+ tst_res(TPASS,
+ "Functionality of setgroups"
+ "(%d, groups_set) successful",
+ gidsetsize);
}
TST_EXP_EQ_LI(groups_get[0], groups_set[0]);
At least that will produce TFAIL results if they don't match, which is
missing here.
Sound reasonable.
- cleanup();
- tst_exit();
}
-/*
- * setup() - performs all ONE TIME setup for this test.
- *
- * Make sure the test process uid is root.
- * Get the supplimentrary group id of test user from /etc/passwd file.
- */
-void setup(void)
+static void setup(void)
{
+ user_info = SAFE_GETPWNAM(TESTUSER);
- tst_require_root();
-
- tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
- TEST_PAUSE;
-
- /* Get the group id info. of TESTUSER from /etc/passwd */
- if ((user_info = getpwnam(TESTUSER)) == NULL) {
- tst_brkm(TFAIL, cleanup, "getpwnam(2) of %s Failed", TESTUSER);
- }
+ GID16_CHECK(user_info->pw_gid, setgroups);
- if (!GID_SIZE_CHECK(user_info->pw_gid)) {
- tst_brkm(TBROK,
- cleanup,
- "gid returned from getpwnam is too large for testing setgroups16");
- }
-
- groups_list[0] = user_info->pw_gid;
+ groups_set[0] = user_info->pw_gid;
Do we really need this? I suppose that any random value will do, e.g.
groups_set[0] = 42;
is going to be as good as anything else.
Agree.
}
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- * completion or premature exit.
- */
-void cleanup(void)
-{
-
-}
+static struct tst_test test = {
+ .test_all = verify_setgroups,
+ .setup = setup,
+ .needs_root = 1,
+};
--
1.8.3.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
Best Regards
Yang Xu
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2023-08-01 11:04 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-19 9:59 [LTP] [PATCH 1/3] syscalls/setgroups01: Convert to new API Yang Xu
2023-07-19 9:59 ` [LTP] [PATCH 2/3] syscalls/setgroups02: " Yang Xu
2023-07-26 12:51 ` Cyril Hrubis
2023-07-26 13:17 ` Cyril Hrubis
2023-08-01 10:52 ` Yang Xu (Fujitsu)
2023-08-01 11:04 ` Yang Xu (Fujitsu)
2023-07-19 9:59 ` [LTP] [PATCH 3/3] syscalls/getgroups03: " Yang Xu
2023-07-26 13:14 ` Cyril Hrubis
2023-08-01 10:53 ` Yang Xu (Fujitsu)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox