From patchwork Thu Jun 14 12:28:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Stancek X-Patchwork-Id: 929397 X-Patchwork-Delegate: petr.vorel@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4162wL1WTbz9s3q for ; Thu, 14 Jun 2018 22:28:34 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 55F881A9640 for ; Thu, 14 Jun 2018 14:28:27 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-3.smtp.seeweb.it (in-3.smtp.seeweb.it [IPv6:2001:4b78:1:20::3]) by picard.linux.it (Postfix) with ESMTP id BD4223E7CDB for ; Thu, 14 Jun 2018 14:28:25 +0200 (CEST) Received: from mx1.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-3.smtp.seeweb.it (Postfix) with ESMTPS id B041D1A00EA1 for ; Thu, 14 Jun 2018 14:28:21 +0200 (CEST) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D73404015A44 for ; Thu, 14 Jun 2018 12:28:19 +0000 (UTC) Received: from dustball.brq.redhat.com (unknown [10.43.17.9]) by smtp.corp.redhat.com (Postfix) with ESMTP id 792ED1C702 for ; Thu, 14 Jun 2018 12:28:19 +0000 (UTC) From: Jan Stancek To: ltp@lists.linux.it Date: Thu, 14 Jun 2018 14:28:13 +0200 Message-Id: <8f4b0a827cd6da1c3774d8fe4f281039b8bce43b.1528979235.git.jstancek@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Thu, 14 Jun 2018 12:28:19 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Thu, 14 Jun 2018 12:28:19 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'jstancek@redhat.com' RCPT:'' X-Virus-Scanned: clamav-milter 0.99.2 at in-3.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=-0.0 required=7.0 tests=SPF_HELO_PASS,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-3.smtp.seeweb.it Subject: [LTP] [PATCH] open08: rewrite to newlib 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: , MIME-Version: 1.0 Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Fixes: https://github.com/linux-test-project/ltp/issues/330 EACCES testcase changed: Test now creates file (0600) in tmpdir, that is owned by privileged user, and unprivileged user tries to open it for writing. uclinux ifdefs dropped. Signed-off-by: Jan Stancek --- testcases/kernel/syscalls/open/open08.c | 137 +++++++++++++------------------- 1 file changed, 55 insertions(+), 82 deletions(-) diff --git a/testcases/kernel/syscalls/open/open08.c b/testcases/kernel/syscalls/open/open08.c index d55d8e6bdd53..8cf44b64063a 100644 --- a/testcases/kernel/syscalls/open/open08.c +++ b/testcases/kernel/syscalls/open/open08.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2013 Wanlong Gao + * Copyright (c) 2018 Linux Test Project * * 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 @@ -39,7 +40,7 @@ * 4. Attempt to open() a filename which is more than VFS_MAXNAMLEN, and * check for errno to be ENAMETOOLONG. * - * 5. Attempt to open a test executable in WRONLY mode, + * 5. Attempt to open a (0600) file owned by different user in WRONLY mode, * open(2) should fail with EACCES. * * 6. Attempt to pass an invalid pathname with an address pointing outside @@ -56,113 +57,85 @@ #include #include #include -#include "test.h" -#include "safe_macros.h" - -static void setup(void); -static void cleanup(void); - -char *TCID = "open08"; - -static char nobody_uid[] = "nobody"; -static struct passwd *ltpuser; - -static char *bad_addr; +#include "tst_test.h" static char filename[40] = ""; -static char fname[] = "/bin/cat"; +static char user2_file[] = "user2_0600"; static char bad_file[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz"; +struct test_case_t; +static void setup_badfname_buf(struct test_case_t *t); + static struct test_case_t { + void (*setup)(struct test_case_t *t); char *fname; int flags; int error; -} TC[] = { - {filename, O_CREAT | O_EXCL, EEXIST}, - {"/tmp", O_RDWR, EISDIR}, - {filename, O_DIRECTORY, ENOTDIR}, - {bad_file, O_RDWR, ENAMETOOLONG}, - {fname, O_WRONLY, EACCES}, -#if !defined(UCLINUX) - {(char *)-1, O_CREAT, EFAULT} -#endif +} tcases[] = { + {NULL, filename, O_CREAT | O_EXCL, EEXIST}, + {NULL, "/tmp", O_RDWR, EISDIR}, + {NULL, filename, O_DIRECTORY, ENOTDIR}, + {NULL, bad_file, O_RDWR, ENAMETOOLONG}, + {NULL, user2_file, O_WRONLY, EACCES}, + {setup_badfname_buf, NULL, O_CREAT, EFAULT} }; -int TST_TOTAL = sizeof(TC) / sizeof(TC[0]); - -int main(int ac, char **av) +void verify_open(unsigned int i) { - 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++) { - TEST(open(TC[i].fname, TC[i].flags, - S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); - - if (TEST_RETURN != -1) { - tst_resm(TFAIL, "call succeeded unexpectedly"); - continue; - } - - if (TEST_ERRNO == TC[i].error) { - tst_resm(TPASS, "expected failure - " - "errno = %d : %s", TEST_ERRNO, - strerror(TEST_ERRNO)); - } else { - tst_resm(TFAIL, "unexpected error - %d : %s - " - "expected %d", TEST_ERRNO, - strerror(TEST_ERRNO), TC[i].error); - } - } + TEST(open(tcases[i].fname, tcases[i].flags, + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); + + if (TEST_RETURN != -1) { + tst_res(TFAIL, "call succeeded unexpectedly"); + return; } - cleanup(); - tst_exit(); + if (TEST_ERRNO == tcases[i].error) { + tst_res(TPASS, "expected failure - " + "errno = %d : %s", TEST_ERRNO, + strerror(TEST_ERRNO)); + } else { + tst_res(TFAIL, "unexpected error - %d : %s - " + "expected %d", TEST_ERRNO, + strerror(TEST_ERRNO), tcases[i].error); + } } -static void setup(void) +static void setup_badfname_buf(struct test_case_t *t) { - int fildes; + if (!t->fname) { + char *bad_addr; - tst_require_root(); + bad_addr = SAFE_MMAP(0, 1, PROT_NONE, + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + t->fname = bad_addr; + } +} - tst_sig(NOFORK, DEF_HANDLER, cleanup); +static void setup(void) +{ + int fildes; + char nobody_uid[] = "nobody"; + struct passwd *ltpuser; umask(0); - TEST_PAUSE; + SAFE_CREAT(user2_file, 0600); /* Switch to nobody user for correct error code collection */ ltpuser = getpwnam(nobody_uid); - SAFE_SETGID(NULL, ltpuser->pw_gid); - SAFE_SETUID(NULL, ltpuser->pw_uid); - - tst_tmpdir(); + SAFE_SETGID(ltpuser->pw_gid); + SAFE_SETUID(ltpuser->pw_uid); sprintf(filename, "open3.%d", getpid()); - - fildes = SAFE_CREAT(cleanup, filename, 0600); - + fildes = SAFE_CREAT(filename, 0600); close(fildes); - -#if !defined(UCLINUX) - bad_addr = mmap(0, 1, PROT_NONE, - MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); - if (bad_addr == MAP_FAILED) - tst_brkm(TBROK, cleanup, "mmap failed"); - - TC[5].fname = bad_addr; -#endif } -static void cleanup(void) -{ - tst_rmdir(); -} +static struct tst_test test = { + .tcnt = ARRAY_SIZE(tcases), + .needs_tmpdir = 1, + .needs_root = 1, + .setup = setup, + .test = verify_open, +};