diff mbox series

setreuid06: Rewrite the test using new LTP API

Message ID 20230112075731.7769-1-akumar@suse.de
State Accepted
Headers show
Series setreuid06: Rewrite the test using new LTP API | expand

Commit Message

Avinesh Kumar Jan. 12, 2023, 7:57 a.m. UTC
Signed-off-by: Avinesh Kumar <akumar@suse.de>
---
 .../kernel/syscalls/setreuid/setreuid06.c     | 108 ++++++------------
 1 file changed, 34 insertions(+), 74 deletions(-)

Comments

Richard Palethorpe Jan. 16, 2023, 11:42 a.m. UTC | #1
Hello,

Merged, with a minor change. See below.

Avinesh Kumar <akumar@suse.de> writes:

> Signed-off-by: Avinesh Kumar <akumar@suse.de>
> ---
>  .../kernel/syscalls/setreuid/setreuid06.c     | 108 ++++++------------
>  1 file changed, 34 insertions(+), 74 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/setreuid/setreuid06.c b/testcases/kernel/syscalls/setreuid/setreuid06.c
> index 22acd996f..72c7e5e47 100644
> --- a/testcases/kernel/syscalls/setreuid/setreuid06.c
> +++ b/testcases/kernel/syscalls/setreuid/setreuid06.c
> @@ -1,95 +1,55 @@
> +// SPDX-License-Identifier: GPL-2.0-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
> - *
>   * Ported by John George
> + * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
>   */
>  
> -/*
> - * Test that EPERM is set when setreuid is given an invalid user id.
> +/*\
> + * [Description]
> + *
> + * Verify that setreuid(2) syscall fails with EPERM errno when the calling
> + * process is not privileged and a change other than
> + * (i) swapping the effective user ID with the real user ID, or
> + * (ii) setting one to the value of the other or
> + * (iii) setting the effective user ID to the value of the saved set-user-ID
> + * was specified.
>   */
>  
> -#include <sys/wait.h>
> -#include <limits.h>
> -#include <signal.h>
> -#include <errno.h>
> -#include <unistd.h>
>  #include <pwd.h>
> -#include <sys/param.h>
> -#include <sys/types.h>
> -#include <sys/stat.h>
> -
> -#include "test.h"
> -#include "safe_macros.h"
> -#include "compat_16.h"
> -
> -#define INVAL_USER		 (USHRT_MAX-2)
> -
> -TCID_DEFINE(setreuid06);
> -int TST_TOTAL = 1;
> +#include "tst_test.h"
> +#include "tst_uid.h"
> +#include "compat_tst_16.h"
>  
>  static struct passwd *ltpuser;
> +static uid_t other_uid;
>  
> -static void setup(void);
> -static void cleanup(void);
> -
> -int main(int argc, char **argv)
> +static void setup(void)
>  {
> -	int lc;
> -
> -	tst_parse_opts(argc, argv, NULL, NULL);
> -
> -	setup();
> +	uid_t test_user[1];
>  
> -	for (lc = 0; TEST_LOOPING(lc); lc++) {
> -		tst_count = 0;
> +	tst_get_uids(test_user, 0, 1);

We can just pass a pointer to other_uid.

Thanks!
diff mbox series

Patch

diff --git a/testcases/kernel/syscalls/setreuid/setreuid06.c b/testcases/kernel/syscalls/setreuid/setreuid06.c
index 22acd996f..72c7e5e47 100644
--- a/testcases/kernel/syscalls/setreuid/setreuid06.c
+++ b/testcases/kernel/syscalls/setreuid/setreuid06.c
@@ -1,95 +1,55 @@ 
+// SPDX-License-Identifier: GPL-2.0-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
- *
  * Ported by John George
+ * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
  */
 
-/*
- * Test that EPERM is set when setreuid is given an invalid user id.
+/*\
+ * [Description]
+ *
+ * Verify that setreuid(2) syscall fails with EPERM errno when the calling
+ * process is not privileged and a change other than
+ * (i) swapping the effective user ID with the real user ID, or
+ * (ii) setting one to the value of the other or
+ * (iii) setting the effective user ID to the value of the saved set-user-ID
+ * was specified.
  */
 
-#include <sys/wait.h>
-#include <limits.h>
-#include <signal.h>
-#include <errno.h>
-#include <unistd.h>
 #include <pwd.h>
-#include <sys/param.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-
-#include "test.h"
-#include "safe_macros.h"
-#include "compat_16.h"
-
-#define INVAL_USER		 (USHRT_MAX-2)
-
-TCID_DEFINE(setreuid06);
-int TST_TOTAL = 1;
+#include "tst_test.h"
+#include "tst_uid.h"
+#include "compat_tst_16.h"
 
 static struct passwd *ltpuser;
+static uid_t other_uid;
 
-static void setup(void);
-static void cleanup(void);
-
-int main(int argc, char **argv)
+static void setup(void)
 {
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
+	uid_t test_user[1];
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
+	tst_get_uids(test_user, 0, 1);
+	other_uid = test_user[0];
 
-		TEST(SETREUID(cleanup, -1, INVAL_USER));
-		if (TEST_RETURN != -1) {
-			tst_resm(TFAIL, "%s did not fail as expected", TCID);
-		} else if (TEST_ERRNO == EPERM) {
-			tst_resm(TPASS, "setreuid set errno to EPERM as "
-				 "expected");
-		} else {
-			tst_resm(TFAIL, "setreuid FAILED, expected 1 but "
-				 "returned %d", TEST_ERRNO);
-		}
+	UID16_CHECK(other_uid, setreuid);
 
-	}
-	cleanup();
-	tst_exit();
+	ltpuser = SAFE_GETPWNAM("nobody");
+	SAFE_SETUID(ltpuser->pw_uid);
 }
 
-static void setup(void)
+static void run(void)
 {
-	tst_require_root();
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
 
-	umask(0);
-
-	ltpuser = getpwnam("nobody");
-	if (ltpuser == NULL)
-		tst_brkm(TBROK, NULL, "nobody must be a valid user.");
-
-	SAFE_SETUID(NULL, ltpuser->pw_uid);
-
-	TEST_PAUSE;
+	TST_EXP_FAIL(SETREUID(-1, other_uid), EPERM,
+				"setreuid(%d, %d)", -1, other_uid);
+	TST_EXP_FAIL(SETREUID(other_uid, -1), EPERM,
+				"setreuid(%d, %d)", other_uid, -1);
+	TST_EXP_FAIL(SETREUID(other_uid, other_uid), EPERM,
+				"setreuid(%d, %d)", other_uid, other_uid);
 }
 
-static void cleanup(void)
-{
-}
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = run,
+	.needs_root = 1
+};