From patchwork Fri Aug 31 07:13:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Yang X-Patchwork-Id: 963774 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=213.254.12.146; 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=cn.fujitsu.com Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 421F8k3fkLz9ryn for ; Thu, 30 Aug 2018 17:52:54 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id ED2CC3E7402 for ; Thu, 30 Aug 2018 09:52:51 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-5.smtp.seeweb.it (in-5.smtp.seeweb.it [IPv6:2001:4b78:1:20::5]) by picard.linux.it (Postfix) with ESMTP id 37C1F3E7402 for ; Thu, 30 Aug 2018 09:52:49 +0200 (CEST) Received: from heian.cn.fujitsu.com (mail.cn.fujitsu.com [183.91.158.132]) by in-5.smtp.seeweb.it (Postfix) with ESMTP id BC4D4600721 for ; Thu, 30 Aug 2018 09:52:47 +0200 (CEST) X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="44179991" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 30 Aug 2018 15:52:46 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id A81E94B6EC67; Thu, 30 Aug 2018 15:52:44 +0800 (CST) Received: from RHEL7U5Alpha_SERVER.g08.fujitsu.local (10.167.220.156) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.408.0; Thu, 30 Aug 2018 15:52:43 +0800 From: Xiao Yang To: Date: Fri, 31 Aug 2018 15:13:22 +0800 Message-ID: <1535699602-28185-2-git-send-email-yangx.jy@cn.fujitsu.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1535699602-28185-1-git-send-email-yangx.jy@cn.fujitsu.com> References: <864746561.43466655.1535545169373.JavaMail.zimbra@redhat.com> <1535699602-28185-1-git-send-email-yangx.jy@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.220.156] X-yoursite-MailScanner-ID: A81E94B6EC67.AB252 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: yangx.jy@cn.fujitsu.com X-Spam-Status: No, score=2.5 required=7.0 tests=DATE_IN_FUTURE_12_24 autolearn=disabled version=3.4.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-5.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Level: ** X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-5.smtp.seeweb.it Cc: ltp@lists.linux.it Subject: [LTP] [PATCH v2 4/4] syscalls/mlock202: Add new testcase 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+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Check various errnos for mlock2(2) since kernel v2.6.9. Note: 1) We use tst_syscall() to check if mlock2() is supported. 2) since kernel v2.6.9, the limits and permissions of mlock2() changed, so we just check mlock2() since the version. Signed-off-by: Xiao Yang --- runtest/ltplite | 1 + runtest/stress.part3 | 1 + runtest/syscalls | 1 + testcases/kernel/syscalls/mlock2/.gitignore | 1 + testcases/kernel/syscalls/mlock2/mlock202.c | 113 ++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 testcases/kernel/syscalls/mlock2/mlock202.c diff --git a/runtest/ltplite b/runtest/ltplite index 270c649..7ebe632 100644 --- a/runtest/ltplite +++ b/runtest/ltplite @@ -444,6 +444,7 @@ mlock01 mlock01 mlock02 mlock02 mlock201 mlock201 +mlock202 mlock202 qmm01 mmap001 -m 1 mmap01 mmap01 diff --git a/runtest/stress.part3 b/runtest/stress.part3 index 6850572..0804172 100644 --- a/runtest/stress.part3 +++ b/runtest/stress.part3 @@ -370,6 +370,7 @@ mlock01 mlock01 mlock02 mlock02 mlock201 mlock201 +mlock202 mlock202 qmm01 mmap001 -m 1 mmap01 mmap01 diff --git a/runtest/syscalls b/runtest/syscalls index 5d84d48..2615254 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -634,6 +634,7 @@ mlock03 mlock03 -i 20 mlock04 mlock04 mlock201 mlock201 +mlock202 mlock202 qmm01 mmap001 -m 1 mmap01 mmap01 diff --git a/testcases/kernel/syscalls/mlock2/.gitignore b/testcases/kernel/syscalls/mlock2/.gitignore index 431eff8..8daf0f9 100644 --- a/testcases/kernel/syscalls/mlock2/.gitignore +++ b/testcases/kernel/syscalls/mlock2/.gitignore @@ -1 +1,2 @@ /mlock201 +/mlock202 diff --git a/testcases/kernel/syscalls/mlock2/mlock202.c b/testcases/kernel/syscalls/mlock2/mlock202.c new file mode 100644 index 0000000..630da65 --- /dev/null +++ b/testcases/kernel/syscalls/mlock2/mlock202.c @@ -0,0 +1,113 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved. + * Author: Xiao Yang + */ +/* + * Description: + * Check various errnos for mlock2(2) since kernel v2.6.9: + * 1) mlock2() fails and returns EINVAL if unknown flag is specified. + * 2) mlock2() fails and returns ENOMEM if the caller is not + * privileged(CAP_IPC_LOCK) and tries to lock more memory than the + * RLIMIT_MEMLOCK limit. + * 3) mlock2() fails and returns EPERM if the caller is not + * privileged(CAP_IPC_LOCK) and its RLIMIT_MEMLOCK limit is 0. + * 4) mlock2() fails and returns ENOMEM if some of the specified address + * range does not correspond to mapped pages in the address space + * of the caller. + */ +#include +#include +#include +#include +#include +#include + +#include "tst_test.h" +#include "lapi/syscalls.h" + +#define PAGES 8 + +static size_t pgsz; +static size_t max_sz1, max_sz2; +static char *addr, *unmapped_addr; +static struct passwd *nobody; + +static struct tcase { + char **taddr; + int flag; + size_t *max_size; + /* 1: nobody 0: root */ + int user; + int exp_err; +} tcases[] = { + {&addr, -1, NULL, 0, EINVAL}, + {&addr, 0, &max_sz1, 1, ENOMEM}, + {&addr, 0, &max_sz2, 1, EPERM}, + {&unmapped_addr, 0, NULL, 0, ENOMEM}, +}; + +static void verify_mlock2(unsigned int n) +{ + struct tcase *tc = &tcases[n]; + struct rlimit orig_limit, new_limit; + + if (tc->user) { + SAFE_GETRLIMIT(RLIMIT_MEMLOCK, &orig_limit); + new_limit.rlim_cur = *tc->max_size; + new_limit.rlim_max = *tc->max_size; + SAFE_SETRLIMIT(RLIMIT_MEMLOCK, &new_limit); + SAFE_SETEUID(nobody->pw_uid); + } + + TEST(tst_syscall(__NR_mlock2, *tc->taddr, pgsz, tc->flag)); + if (TST_RET != -1) { + tst_res(TFAIL, "mlock2() succeeded"); + SAFE_MUNLOCK(*tc->taddr, pgsz); + goto end; + } + + if (TST_ERR != tc->exp_err) { + tst_res(TFAIL | TTERRNO, + "mlock2() failed unexpectedly, expected %s", + tst_strerrno(tc->exp_err)); + } else { + tst_res(TPASS | TTERRNO, "mlock2() failed as expected"); + } + +end: + if (tc->user) { + SAFE_SETEUID(0); + SAFE_SETRLIMIT(RLIMIT_MEMLOCK, &orig_limit); + } +} + +static void setup(void) +{ + pgsz = getpagesize(); + nobody = SAFE_GETPWNAM("nobody"); + + addr = SAFE_MMAP(NULL, pgsz, PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + unmapped_addr = SAFE_MMAP(NULL, pgsz * PAGES, PROT_READ, + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + SAFE_MUNMAP(unmapped_addr, pgsz * PAGES); + unmapped_addr = unmapped_addr + pgsz * PAGES / 2; + + max_sz1 = pgsz - 1; +} + +static void cleanup(void) +{ + if (addr) + SAFE_MUNMAP(addr, pgsz); +} + +static struct tst_test test = { + .tcnt = ARRAY_SIZE(tcases), + .test = verify_mlock2, + .setup = setup, + .cleanup = cleanup, + .needs_root = 1, + .min_kver = "2.6.9", +};