From patchwork Tue Nov 6 16:11:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sandeep Patil X-Patchwork-Id: 150281 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp4073834ljp; Tue, 6 Nov 2018 08:11:49 -0800 (PST) X-Google-Smtp-Source: AJdET5dRILY821UkUxZH+7w/Q6x5PxTQbShTTLSdEd6tNXByuaRmHSS7rYBGw1/30ro4Eg3BCKF4 X-Received: by 2002:a05:6402:7d4:: with SMTP id u20mr23024007edy.81.1541520709367; Tue, 06 Nov 2018 08:11:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541520709; cv=none; d=google.com; s=arc-20160816; b=lqZ2vIJxzpfK37Khlv2CScrQQ98lmsNNySEOgH6bJ37RB3W0Eb1EAfxFZ7aO7JjgZB RABUJ5BM3CkBw1nGoa2bm/x9MFCB9otqlcrt/bV9RXx56SqOrOUpslQ4+lolQgRQd+/j N2chUPbFmq56D1coWHVD9ei6lKOdMqQ7e8FKznoSDuM1CoGuZH9bAxY6YTk7wvEuQGP0 CQiDA9Ox+7abKx+jjrWn8WCWDC7OvHesaLtKWZrajaNQEne9QjE4uM5r5b+OA174HHKm 4ZG/9Yf2JyPT7esevH8UcWqmJxo643AsVFQM5xvSeO6ltEtn3Q7rynrhcroljn1VCKLI EVBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :cc:mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:delivered-to; bh=rt3nRhAWIsZqXsDX8GLxaSbxtQ8n5xBzJu/H2brE4Wg=; b=C4d7T5MWvp/0MVc9V84lRcbYEzdSp5Xjz2ChENzPWEpmyjCqkVxPrmHdN19MLxGhmm gNBLpv/ZrzewCsHDm9X5LLNdVA0oidWaYdc9S545WoP1I9NqZ59c+NOuXl3hmbUEXdq0 3+Cm4NAw1tJgl8cdo+6qpX4b4Jc++d3aaHDn5H3lDT7PBVRniRqb9kwqLVNd7jfHGhzK ca0T3sjG0ecnTPgo8UAYMZlgloNL/mOl19jR3H1mNjcfkmUltP/5Nz0U4dV9hOlzmHrN iA89pnPV7vhwqGCzfIqrLRv6p4JZQRlrlig1IWDi0iEHWhJZe3uBNbwemmt9j2vjKVnX Ku0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com header.s=20161025 header.b=UQshivbO; spf=pass (google.com: domain of ltp-bounces+patch=linaro.org@lists.linux.it designates 213.254.12.146 as permitted sender) smtp.mailfrom="ltp-bounces+patch=linaro.org@lists.linux.it"; dmarc=fail (p=REJECT sp=REJECT dis=QUARANTINE) header.from=google.com Return-Path: Received: from picard.linux.it (picard.linux.it. [213.254.12.146]) by mx.google.com with ESMTPS id f1si4902724edx.146.2018.11.06.08.11.49 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Nov 2018 08:11:49 -0800 (PST) Received-SPF: pass (google.com: domain of ltp-bounces+patch=linaro.org@lists.linux.it designates 213.254.12.146 as permitted sender) client-ip=213.254.12.146; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com header.s=20161025 header.b=UQshivbO; spf=pass (google.com: domain of ltp-bounces+patch=linaro.org@lists.linux.it designates 213.254.12.146 as permitted sender) smtp.mailfrom="ltp-bounces+patch=linaro.org@lists.linux.it"; dmarc=fail (p=REJECT sp=REJECT dis=QUARANTINE) header.from=google.com Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id F0AEC3E7693 for ; Tue, 6 Nov 2018 17:11:48 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-4.smtp.seeweb.it (in-4.smtp.seeweb.it [IPv6:2001:4b78:1:20::4]) by picard.linux.it (Postfix) with ESMTP id E12E13E7726 for ; Tue, 6 Nov 2018 17:11:45 +0100 (CET) Received: from mail-pl1-x643.google.com (mail-pl1-x643.google.com [IPv6:2607:f8b0:4864:20::643]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by in-4.smtp.seeweb.it (Postfix) with ESMTPS id B26AC1000520 for ; Tue, 6 Nov 2018 17:11:44 +0100 (CET) Received: by mail-pl1-x643.google.com with SMTP id w22-v6so3097426plk.0 for ; Tue, 06 Nov 2018 08:11:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4WWX+0NWtvXcbX0O6EMWxqbl32cDfRn2AX4vhv8VvJE=; b=UQshivbOpu5Kl+jq4b5DtaG7kD6qDlsQ1GX0avB/lq2DD9hFyqj4AB2iG2qy6Ub7z1 yWTGcJbH1tcATjy44MkjMSyy3u5CBOHCWPRUQGf9xl5Vtv3yTlfvOMkiLpCU2zIHnwxY gY5tEhLwTYSthILEAtYGXiJd1U3ZEJT4nwGzOFsuJshRAnO7lnd7i5nJHLAcMHoAGvaN PxwZqZIf1xdSNrOHxHqSVJtvv7bOtNuf/+ZcavOuTAuUAZTz+fnhgxY+fk/z+xi30THy PP4sfvLL3wg0CGCBXZK1Xb1oEiJKfQCvvg4rbmP2fExJA2mrdn9lh4Dv3v7AaP5/F4ji pebA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4WWX+0NWtvXcbX0O6EMWxqbl32cDfRn2AX4vhv8VvJE=; b=iRTmgiHJoZkfFUdZfAgLCUrNnVu8c+mEn4C8wpHiJnWY0YjLNHVSZL5GwWDcm/qVGh LoeUyKsGW8gp98Zth8IlFMNraMp7n3ze9m8sG3QfTRXgCj66oWDW+5NnaLBHx0JLBoMO z9IBKJ6ns5QMv4V8z9MOw8eUhEakQ4rNtOQLxmO4QRs7mIBa7W53HUBKV3xBvrqvWTVE MKAEmSN+xIP8hsxRUvvEg6D87B033K83Cyb5Ax24GklIyAV5QF3ntyd5pLM/NnLHwoS0 8kdx3jwi64fQnsqOKEX+W8aiFcEvPWXxBYY8snkLLWCR51na+PT7wsBGPlOlZdPgms4l zu2w== X-Gm-Message-State: AGRZ1gKc+kKf0VNLN1BpuoxdPMO9xwRujoybRSaWfjS9vQcBRoKKG+jX ab26/BJYAgW64+6Y40/plF++aqZ0HwdHWw== X-Received: by 2002:a17:902:8e81:: with SMTP id bg1-v6mr8536185plb.192.1541520702007; Tue, 06 Nov 2018 08:11:42 -0800 (PST) Received: from sspatil-workstation.mtv.corp.google.com ([2620:0:1000:1601:1a43:e60e:c432:d19c]) by smtp.gmail.com with ESMTPSA id m7-v6sm52232602pgq.59.2018.11.06.08.11.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Nov 2018 08:11:39 -0800 (PST) From: Sandeep Patil To: ltp@lists.linux.it Date: Tue, 6 Nov 2018 08:11:26 -0800 Message-Id: <20181106161126.251484-3-sspatil@google.com> X-Mailer: git-send-email 2.19.1.930.g4563a0d9d0-goog In-Reply-To: <20181106161126.251484-1-sspatil@google.com> References: <20181106161126.251484-1-sspatil@google.com> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-4.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=-14.9 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,ENV_AND_HDR_SPF_MATCH,SPF_PASS,USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-4.smtp.seeweb.it Cc: kernel-team@android.com, rpalethorpe@suse.com Subject: [LTP] [PATCH 2/2] syscalls/chmod05: Use new ltp library X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.18 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+patch=linaro.org@lists.linux.it Sender: "ltp" Use SPDX-Licence-Identifier and delete dead comments / code while at it. Signed-off-by: Sandeep Patil --- v1->v2: ------ - Fix SPDX identifier to make it GPLv2 or later. - Fix build errors (missed cause 'make install' doesn't build things again) - Add cleanup function to reset the effective uid / gid. testcases/kernel/syscalls/chmod/chmod05.c | 191 +++++----------------- 1 file changed, 39 insertions(+), 152 deletions(-) diff --git a/testcases/kernel/syscalls/chmod/chmod05.c b/testcases/kernel/syscalls/chmod/chmod05.c index 3cf4db5d0..0e86f0691 100644 --- a/testcases/kernel/syscalls/chmod/chmod05.c +++ b/testcases/kernel/syscalls/chmod/chmod05.c @@ -1,30 +1,12 @@ +/// 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 */ /* * Test Name: chmod05 * * Test Description: -//wjh -//This test seems to be invalid see comments below -//The man page for chmod doesn't seem to indicate that the setgid bit can -//only be set by root * Verify that, chmod(2) will succeed to change the mode of a directory * but fails to set the setgid bit on it if invoked by non-root (uid != 0) * process with the following constraints, @@ -36,48 +18,6 @@ * chmod() should return value 0 on success and though succeeds to change * the mode of a directory but fails to set setgid bit on it. * - * Algorithm: - * Setup: - * Setup signal handling. - * Create temporary directory. - * 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, - * 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 - * Delete the temporary directory created. - * - * Usage: - * chmod05 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t] - * where, -c n : Run n copies concurrently. - * -e : Turn on errno logging. - * -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 - * 05/2002 Fixes by wjhuie and Ferhan Shareef - * changed conditional to check for valid tests only and - * in a more logically understandable way - * 07/2002 Additional fixes to #defines to allow comparisions to work. - * -Robbie Williamson - * - * RESTRICTIONS: - * This test should be run by root. - * */ #ifndef _GNU_SOURCE @@ -96,126 +36,73 @@ #include #include -#include "test.h" -#include "safe_macros.h" - -#define DEBUG 0 +#include "tst_test.h" #define MODE_RWX (mode_t)(S_IRWXU | S_IRWXG | S_IRWXO) #define DIR_MODE (mode_t)(S_ISVTX | S_ISGID | S_IFDIR) #define PERMS (mode_t)(MODE_RWX | DIR_MODE) #define TESTDIR "testdir" -char *TCID = "chmod05"; -int TST_TOTAL = 1; - -void setup(); /* Main setup function for test */ -void cleanup(); /* Main cleanup function for test */ - -int main(int ac, char **av) +void test_chmod(void) { struct stat stat_buf; /* stat struct */ - int lc; mode_t dir_mode; /* mode permissions set on test directory */ - tst_parse_opts(ac, av, NULL, NULL); - - setup(); - - for (lc = 0; TEST_LOOPING(lc); lc++) { - - tst_count = 0; - - TEST(chmod(TESTDIR, PERMS)); - - if (TEST_RETURN == -1) { - tst_resm(TFAIL, "chmod(%s, %#o) failed", - TESTDIR, PERMS); - continue; - } - /* - * Get the directory information using - * stat(2). - */ - if (stat(TESTDIR, &stat_buf) < 0) { - tst_brkm(TFAIL | TERRNO, cleanup, - "stat(%s) failed", TESTDIR); - } - dir_mode = stat_buf.st_mode; -#if DEBUG - printf("DIR_MODE = 0%03o\n", DIR_MODE); - printf("MODE_RWX = 0%03o\n", MODE_RWX); - printf("PERMS = 0%03o\n", PERMS); - printf("dir_mode = 0%03o\n", dir_mode); -#endif - if ((PERMS & ~S_ISGID) != dir_mode) - tst_resm(TFAIL, "%s: Incorrect modes 0%03o, " - "Expected 0%03o", TESTDIR, dir_mode, - PERMS & ~S_ISGID); - else - tst_resm(TPASS, - "Functionality of chmod(%s, %#o) successful", - TESTDIR, PERMS); + TEST(chmod(TESTDIR, PERMS)); + if (TST_RET == -1) { + tst_res(TFAIL, "chmod(%s, %#o) failed", TESTDIR, PERMS); } - cleanup(); - tst_exit(); + /* Get the directory information using stat(2) and compare with + * expected results. + */ + SAFE_STAT(TESTDIR, &stat_buf); + dir_mode = stat_buf.st_mode; + if ((PERMS & ~S_ISGID) != dir_mode) { + tst_res(TFAIL, "%s: Incorrect modes 0%03o, " + "Expected 0%03o", TESTDIR, dir_mode, + PERMS & ~S_ISGID); + } else { + tst_res(TPASS, "Functionality of chmod(%s, %#o) successful", + TESTDIR, PERMS); + } } -/* - * void - * setup() - performs all ONE TIME setup for this test. - * Create a temporary directory and cd to it. - * Create a test directory under temporary directory. -//wjh we are root so do we really need this kluged helper program? - * Invoke setuid to root program to modify group ownership - * on test directory. - */ void setup(void) { struct passwd *nobody_u; - struct group *bin_group; + struct group *bin_gr; - tst_require_root(); - - TEST_PAUSE; - - tst_tmpdir(); - - nobody_u = getpwnam("nobody"); - if (nobody_u == NULL) - tst_brkm(TBROK | TERRNO, cleanup, - "getpwnam(\"nobody\") failed"); - - bin_group = getgrnam("bin"); - if (bin_group == NULL) - tst_brkm(TBROK | TERRNO, cleanup, "getgrnam(\"bin\") failed"); + nobody_u = SAFE_GETPWNAM("nobody"); + bin_gr = SAFE_GETGRNAM("bin"); /* * Create a test directory under temporary directory with specified * mode permissions and change the gid of test directory to nobody's * gid. */ - SAFE_MKDIR(cleanup, TESTDIR, MODE_RWX); - + SAFE_MKDIR(TESTDIR, MODE_RWX); if (setgroups(1, &nobody_u->pw_gid) == -1) - tst_brkm(TBROK | TERRNO, cleanup, - "setgroups to nobody's gid failed"); + tst_brk(TBROK | TERRNO, "setgroups to nobody's gid failed"); - SAFE_CHOWN(cleanup, TESTDIR, nobody_u->pw_uid, bin_group->gr_gid); + SAFE_CHOWN(TESTDIR, nobody_u->pw_uid, bin_gr->gr_gid); /* change to nobody:nobody */ - if (setegid(nobody_u->pw_gid) == -1 || seteuid(nobody_u->pw_uid) == -1) - tst_brkm(TBROK | TERRNO, cleanup, - "Couldn't switch to nobody:nobody"); + SAFE_SETEGID(nobody_u->pw_gid); + SAFE_SETEUID(nobody_u->pw_uid); } -void cleanup(void) +void cleanup_chmod(void) { - if (setegid(0) == -1) - tst_resm(TWARN | TERRNO, "setegid(0) failed"); - if (seteuid(0) == -1) - tst_resm(TWARN | TERRNO, "seteuid(0) failed"); - - tst_rmdir(); + /* Reset the uid / gid to root */ + SAFE_SETEGID(0); + SAFE_SETEUID(0); } + +static struct tst_test test = { + .needs_root = 1, + .needs_tmpdir = 1, + .setup = setup, + .test_all = test_chmod, + .cleanup = cleanup_chmod, +};