From patchwork Tue Aug 21 09:38:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Yang X-Patchwork-Id: 960175 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=cn.fujitsu.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 41vly23Rwrz9s78 for ; Tue, 21 Aug 2018 19:39:38 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id B7DF53E7594 for ; Tue, 21 Aug 2018 11:39:35 +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 [217.194.8.4]) by picard.linux.it (Postfix) with ESMTP id 703873E63FF for ; Tue, 21 Aug 2018 11:39:23 +0200 (CEST) Received: from heian.cn.fujitsu.com (mail.cn.fujitsu.com [183.91.158.132]) by in-4.smtp.seeweb.it (Postfix) with ESMTP id 56C821000C00 for ; Tue, 21 Aug 2018 11:39:21 +0200 (CEST) X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="43735943" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 21 Aug 2018 17:39:13 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id 0BE1C4B6AE02 for ; Tue, 21 Aug 2018 17:39:08 +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; Tue, 21 Aug 2018 17:39:09 +0800 From: Xiao Yang To: Date: Tue, 21 Aug 2018 17:38:59 +0800 Message-ID: <1534844339-8655-4-git-send-email-yangx.jy@cn.fujitsu.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1534844339-8655-1-git-send-email-yangx.jy@cn.fujitsu.com> References: <1534844339-8655-1-git-send-email-yangx.jy@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.220.156] X-yoursite-MailScanner-ID: 0BE1C4B6AE02.A9BCF X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: yangx.jy@cn.fujitsu.com X-Spam-Status: No, score=0.0 required=7.0 tests=none autolearn=disabled version=3.4.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-4.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-4.smtp.seeweb.it Subject: [LTP] [PATCH 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. 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 | 110 ++++++++++++++++++++++++++++ 5 files changed, 114 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..61dd29d --- /dev/null +++ b/testcases/kernel/syscalls/mlock2/mlock202.c @@ -0,0 +1,110 @@ +// 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) mlock(2) fails and returns EINVAL if unknown flag is specified. + * 2) mlock(2) 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) mlock(2) fails and returns EPERM if the caller is not + * privileged(CAP_IPC_LOCK) and its RLIMIT_MEMLOCK limit is 0. + * 4) mlock(2) 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" + +static unsigned long pgsz; +static unsigned long max_sz1, max_sz2; +static char *addr, *unmapped_addr; +static struct passwd *nobody; + +static struct tcase { + char **taddr; + int flag; + unsigned long *max_size; + /* 1: nobody expected 0: root expected */ + int exp_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->exp_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->exp_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, PROT_READ, + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + SAFE_MUNMAP(unmapped_addr, pgsz); + + 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", +};