Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp2889861rdh; Mon, 30 Oct 2023 10:28:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHHGZbJ4TVsFZ0yUtEz2D7i/HtUTktaplr6xayD9fXlVDu8X1x3OxwjW8XJLleVaTZDmxhO X-Received: by 2002:a05:6a00:93a5:b0:6b3:80f8:7e91 with SMTP id ka37-20020a056a0093a500b006b380f87e91mr342392pfb.9.1698686915631; Mon, 30 Oct 2023 10:28:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698686915; cv=none; d=google.com; s=arc-20160816; b=Y5Og96V5938VVvMjFWvdUVYFNlEiKRAZeh4htcAtra6R+vG8zAyUaQrTs1wYaAIry+ 5dTMfM/7klRm2vLgoPiR7tFRMIpdeUTB8io7oa2a738wq9js5xAg4cJgpWXWw6lUFXoU KC6eWrCtcarqrRblX7hs1rQlTxVAn+czhZSlpbDM3ZAhQiShV7tgJ/kN5MA3Zlvvv+k+ PNOxwIxzjg8pJfxQaHJ+DPCL6s40TqHkz3bikCQzBfulFRP8dug619xjv6EX4KvYiXQp FIInIch3Fjd9SCzxsGHDEAP89Vp7z8n+Nw4OtjHmBpE1OhhzS5oDtAPAVe6eASPpwHKw K5mw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=gBOyFmhp300ivVzEoJ9WI36qh/bXE0ad6fTBo+6DYnM=; fh=2EmMQjstHfXsQK2+UrrrrfFW0V32hQkXdDrw9uHwN2M=; b=gjXDnVTOMINmLzbv9f9mCagVWguFpQCygx5Ym8pYppvMLinOmebUuM4H/FW7OQ5mfQ fmlVkhrxYRluM8voGwuuM9+viE7xQNUXc/bbr6ZvmgRB7YwXYhkMaiFMRLlo6nIFjkG/ f54UCOq7RuFfeTeF+BG4z3pvN5Lvs4mFRccBkWFs3qlU0UD95pmZzFo4dkmeHKP8OsR+ 384wq1cp8EHYu8aVaxU43BRkV8lRzoTwPGGaM4FGj0TCXQEuXBvrE9KTqME4ovMMJ/+1 FIVgQuRCN/tGilVV1VT4u3OD2qr32twR8EvX23JTkku+h/jRMJoYNuL15yzBL+aUOlGq 7ECw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="Aw/A6K5i"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id h5-20020a056a001a4500b006bdfb718e16si5261954pfv.80.2023.10.30.10.28.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Oct 2023 10:28:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="Aw/A6K5i"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 89ED380B5A33; Mon, 30 Oct 2023 10:28:31 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231167AbjJ3R2W (ORCPT + 99 others); Mon, 30 Oct 2023 13:28:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41078 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbjJ3R2V (ORCPT ); Mon, 30 Oct 2023 13:28:21 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E54CD93 for ; Mon, 30 Oct 2023 10:28:18 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-2800f7c8125so2175943a91.1 for ; Mon, 30 Oct 2023 10:28:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698686898; x=1699291698; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=gBOyFmhp300ivVzEoJ9WI36qh/bXE0ad6fTBo+6DYnM=; b=Aw/A6K5ijrG1HKbcw+/Wkt5J8FQUyIBelHb5X9FImBZLnZb0N8zWS7XYjcV0ce6OI5 7FaaBV4qTwjQ07208NLwiUHWFMKzvsJx8uQCThIZc3eol8MvfyWarIkR+x0MDGILTXG3 2Vg8QAMx3oo5sDI14CBLN2HRqyMKyVt4s3t4SOmFY6tCbyLuCVo5hR3b+NHC3RJ3LVu1 9bBxufK0aVyjtHTSQ9alnTjW6JavHdeDFMeO12qwGM6CQjoVcdpAl3cT2cbF9eEicAI1 35uc2aaqxTgTdZIhqhxJWInyuvWPsuRsnSJmtscW3yjbrAg7G2qZ8jx7ii0MpYCeQ5AR WUxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698686898; x=1699291698; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=gBOyFmhp300ivVzEoJ9WI36qh/bXE0ad6fTBo+6DYnM=; b=jOb4knirX+0HJ8jR1+2vKpB4qCUwOPHPMtF6aJLuszT6RoCY8UfdpK74VeHDHKwfzM eUxnsJwKWmJYmYToVuB6n8HVqNubknbZK3BiZ908FZANFufB6jsnj/ECSdUGvYAYdxri rbZIcFo6cmaiMT4Np2PMxrcQ5bTSDhBZLHoY2Fljo9ORfTl3MbABVYDdWT52zZf+NxKQ gQSge90GE+sF0j83RNE8/xyLa75NUmHPZT4Qag2uxxK65LA1M69CCbeXXbHgsgVj5S7K 723IGofDTQ+Hfq++hocuyMLTJ1BPe+t4yO2N92Zw1zAGD1/U3nX3nVZQCz4hUULDdaoU h8TQ== X-Gm-Message-State: AOJu0Yx7Y164SelEi7+mUk+AdG1E0CcdLPACt6guZmCaiJCkuayORrAD +1Gy+r8sjefzmwxPdNXwKfDjkkBOXCM= X-Received: by 2002:a17:90b:4b92:b0:280:982:aa9f with SMTP id lr18-20020a17090b4b9200b002800982aa9fmr338767pjb.7.1698686898260; Mon, 30 Oct 2023 10:28:18 -0700 (PDT) Received: from swarup-virtual-machine ([171.76.83.22]) by smtp.gmail.com with ESMTPSA id rj6-20020a17090b3e8600b002809074eb3esm427652pjb.21.2023.10.30.10.28.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Oct 2023 10:28:17 -0700 (PDT) Date: Mon, 30 Oct 2023 22:58:12 +0530 From: swarup To: Alexey Dobriyan Cc: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/2] proc: test ProtectionKey in proc-empty-vm test Message-ID: References: <76f42cce-b1ab-45ec-b6b2-4c64f0dccb90@p183> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 30 Oct 2023 10:28:31 -0700 (PDT) esOn Fri, Oct 27, 2023 at 05:26:24PM +0300, Alexey Dobriyan wrote: > From: Swarup Laxman Kotiaklapudi > > Check ProtectionKey field in /proc/*/smaps output, if system supports > protection keys feature. > > [test support in the beginning of the program, > use syscall, not glibc pkey_alloc(3) which may not compile, > --adobriyan] > > Signed-off-by: Swarup Laxman Kotiaklapudi > Signed-off-by: Alexey Dobriyan > --- > > tools/testing/selftests/proc/proc-empty-vm.c | 79 ++++++++++++++++++++------- > 1 file changed, 61 insertions(+), 18 deletions(-) > > --- a/tools/testing/selftests/proc/proc-empty-vm.c > +++ b/tools/testing/selftests/proc/proc-empty-vm.c > @@ -23,6 +23,9 @@ > * /proc/${pid}/smaps > * /proc/${pid}/smaps_rollup > */ > +#undef _GNU_SOURCE > +#define _GNU_SOURCE > + > #undef NDEBUG > #include > #include > @@ -34,6 +37,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -42,6 +46,43 @@ > #define TEST_VSYSCALL > #endif > > +#if defined __amd64__ > + #ifndef SYS_pkey_alloc > + #define SYS_pkey_alloc 330 > + #endif > + #ifndef SYS_pkey_free > + #define SYS_pkey_free 331 > + #endif > +#elif defined __i386__ > + #ifndef SYS_pkey_alloc > + #define SYS_pkey_alloc 381 > + #endif > + #ifndef SYS_pkey_free > + #define SYS_pkey_free 382 > + #endif > +#else > + #error "SYS_pkey_alloc" > +#endif > + > +static int g_protection_key_support; > + > +static int protection_key_support(void) > +{ > + long rv = syscall(SYS_pkey_alloc, 0, 0); > + if (rv > 0) { > + syscall(SYS_pkey_free, (int)rv); > + return 1; > + } else if (rv == -1 && errno == ENOSYS) { > + return 0; > + } else if (rv == -1 && errno == EINVAL) { > + // ospke=n > + return 0; > + } else { > + fprintf(stderr, "%s: error: rv %ld, errno %d\n", __func__, rv, errno); > + exit(EXIT_FAILURE); > + } > +} > + > /* > * 0: vsyscall VMA doesn't exist vsyscall=none > * 1: vsyscall VMA is --xp vsyscall=xonly > @@ -84,10 +125,6 @@ static const char proc_pid_smaps_vsyscall_1[] = > "SwapPss: 0 kB\n" > "Locked: 0 kB\n" > "THPeligible: 0\n" > -/* > - * "ProtectionKey:" field is conditional. It is possible to check it as well, > - * but I don't have such machine. > - */ > ; > > static const char proc_pid_smaps_vsyscall_2[] = > @@ -115,10 +152,6 @@ static const char proc_pid_smaps_vsyscall_2[] = > "SwapPss: 0 kB\n" > "Locked: 0 kB\n" > "THPeligible: 0\n" > -/* > - * "ProtectionKey:" field is conditional. It is possible to check it as well, > - * but I'm too tired. > - */ > ; > > static void sigaction_SIGSEGV(int _, siginfo_t *__, void *___) > @@ -240,19 +273,27 @@ static int test_proc_pid_smaps(pid_t pid) > } > perror("open /proc/${pid}/smaps"); > return EXIT_FAILURE; > + } > + ssize_t rv = read(fd, buf, sizeof(buf)); > + close(fd); > + > + assert(0 <= rv); > + assert(rv <= sizeof(buf)); > + > + if (g_vsyscall == 0) { > + assert(rv == 0); > } else { > - ssize_t rv = read(fd, buf, sizeof(buf)); > - close(fd); > - if (g_vsyscall == 0) { > - assert(rv == 0); > - } else { > - size_t len = strlen(g_proc_pid_smaps_vsyscall); > - /* TODO "ProtectionKey:" */ > - assert(rv > len); > - assert(memcmp(buf, g_proc_pid_smaps_vsyscall, len) == 0); > + size_t len = strlen(g_proc_pid_smaps_vsyscall); > + assert(rv > len); > + assert(memcmp(buf, g_proc_pid_smaps_vsyscall, len) == 0); > + > + if (g_protection_key_support) { > +#define PROTECTION_KEY "ProtectionKey: 0\n" > + assert(memmem(buf, rv, PROTECTION_KEY, strlen(PROTECTION_KEY))); > } > - return EXIT_SUCCESS; > } > + > + return EXIT_SUCCESS; > } > > static const char g_smaps_rollup[] = > @@ -419,6 +460,8 @@ int main(void) > abort(); > } > > + g_protection_key_support = protection_key_support(); > + > pid_t pid = fork(); > if (pid == -1) { > perror("fork"); Reviewed-by: Swarup Laxman Kotikalapudi Tested-by: Swarup Laxman Kotikalapudi Hi Alexey, Thanks a lot for correcting the fix. Thanks, Swarup