From patchwork Fri Aug 23 09:46:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Palethorpe X-Patchwork-Id: 1152057 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=none (p=none dis=none) header.from=suse.com Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46FGlG74mFz9sNk for ; Fri, 23 Aug 2019 19:47:04 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 6E7283C1CF3 for ; Fri, 23 Aug 2019 11:47:00 +0200 (CEST) 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 8EC943C1770 for ; Fri, 23 Aug 2019 11:46:58 +0200 (CEST) Received: from mx1.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-4.smtp.seeweb.it (Postfix) with ESMTPS id 05093100159A for ; Fri, 23 Aug 2019 11:46:49 +0200 (CEST) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 6B17CAF8E; Fri, 23 Aug 2019 09:46:55 +0000 (UTC) From: Richard Palethorpe To: ltp@lists.linux.it Date: Fri, 23 Aug 2019 11:46:21 +0200 Message-Id: <20190823094621.21747-2-rpalethorpe@suse.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190823094621.21747-1-rpalethorpe@suse.com> References: <20190823094621.21747-1-rpalethorpe@suse.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=0.0 required=7.0 tests=SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-4.smtp.seeweb.it Subject: [LTP] [PATCH v3 2/2] capability: library tests X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mmoese@suse.com, Richard Palethorpe Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Signed-off-by: Richard Palethorpe Reviewed-by: Yang Xu Reviewed-by: Petr Vorel --- include/lapi/capability.h | 8 +++++ lib/newlib_tests/tst_capability01.c | 51 +++++++++++++++++++++++++++++ lib/newlib_tests/tst_capability02.c | 35 ++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 lib/newlib_tests/tst_capability01.c create mode 100644 lib/newlib_tests/tst_capability02.c diff --git a/include/lapi/capability.h b/include/lapi/capability.h index 02d7a9fda..dac233d84 100644 --- a/include/lapi/capability.h +++ b/include/lapi/capability.h @@ -12,10 +12,18 @@ # include #endif +#ifndef CAP_NET_RAW +# define CAP_NET_RAW 13 +#endif + #ifndef CAP_SYS_ADMIN # define CAP_SYS_ADMIN 21 #endif +#ifndef CAP_AUDIT_READ +# define CAP_AUDIT_READ 37 +#endif + #ifndef CAP_TO_INDEX # define CAP_TO_INDEX(x) ((x) >> 5) #endif diff --git a/lib/newlib_tests/tst_capability01.c b/lib/newlib_tests/tst_capability01.c new file mode 100644 index 000000000..4057549bf --- /dev/null +++ b/lib/newlib_tests/tst_capability01.c @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (c) 2019 Richard Palethorpe + * + * The user or file requires CAP_NET_RAW for this test to work. + * e.g use "$ setcap cap_net_raw=pei tst_capability" + */ + +#include +#include + +#include "tst_test.h" +#include "tst_capability.h" +#include "tst_safe_net.h" + +#include "lapi/socket.h" + +static void run(void) +{ + TEST(socket(AF_INET, SOCK_RAW, 1)); + if (TST_RET > -1) { + tst_res(TFAIL, "Created raw socket"); + SAFE_CLOSE(TST_RET); + } else if (TST_ERR != EPERM) { + tst_res(TBROK | TTERRNO, + "Failed to create socket for wrong reason"); + } else { + tst_res(TPASS | TTERRNO, "Didn't create raw socket"); + } +} + +static void setup(void) +{ + if (geteuid() == 0) + tst_res(TWARN, "CAP_NET_RAW may be ignored when euid == 0"); + + TEST(socket(AF_INET, SOCK_RAW, 1)); + if (TST_RET < 0) + tst_brk(TFAIL | TTERRNO, "Can't create raw socket in setup"); + + SAFE_CLOSE(TST_RET); +} + +static struct tst_test test = { + .setup = setup, + .test_all = run, + .caps = (struct tst_cap []) { + TST_CAP(TST_CAP_DROP, CAP_NET_RAW), + {} + }, +}; diff --git a/lib/newlib_tests/tst_capability02.c b/lib/newlib_tests/tst_capability02.c new file mode 100644 index 000000000..45e3f2d22 --- /dev/null +++ b/lib/newlib_tests/tst_capability02.c @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (c) 2019 Richard Palethorpe + */ + +#include +#include + +#include "tst_test.h" +#include "tst_capability.h" +#include "tst_safe_net.h" + +#include "lapi/socket.h" + +static void run(void) +{ + TEST(socket(AF_INET, SOCK_RAW, 1)); + if (TST_RET > -1) { + tst_res(TPASS, "Created raw socket"); + SAFE_CLOSE(TST_RET); + } else { + tst_res(TFAIL | TTERRNO, "Didn't create raw socket"); + } +} + +static struct tst_test test = { + .test_all = run, + .needs_root = 1, + .caps = (struct tst_cap []) { + TST_CAP(TST_CAP_REQ, CAP_NET_RAW), + TST_CAP(TST_CAP_DROP, CAP_AUDIT_READ), /* 64bit capability */ + TST_CAP(TST_CAP_DROP, CAP_SYS_ADMIN), + {} + }, +};