All of lore.kernel.org
 help / color / mirror / Atom feed
From: Avinesh Kumar <akumar@suse.de>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH] setreuid06: Rewrite the test using new LTP API
Date: Thu, 12 Jan 2023 13:27:31 +0530	[thread overview]
Message-ID: <20230112075731.7769-1-akumar@suse.de> (raw)

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);
+	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
+};
-- 
2.39.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

             reply	other threads:[~2023-01-12  7:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-12  7:57 Avinesh Kumar [this message]
2023-01-16 11:42 ` [LTP] [PATCH] setreuid06: Rewrite the test using new LTP API Richard Palethorpe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230112075731.7769-1-akumar@suse.de \
    --to=akumar@suse.de \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.