From patchwork Wed Feb 14 15:11:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 200992 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1293232dyb; Wed, 14 Feb 2024 07:31:39 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXacEoa+qcfwdFTO4HAJZ+QeDPp41eaGV7VcjPfLJaKYslB6fPzav+TGjfhjZbtyqgywcBOzA1J+vTZoiz8sDdNh0BpMA== X-Google-Smtp-Source: AGHT+IG54k8Blp7ggqx8jn697inyzcIXUz3kgIG9gO2cUU7WKnHh5Q1ouMzTOzW6VJ9NME7egPvl X-Received: by 2002:a17:902:a386:b0:1d9:1cb5:558b with SMTP id x6-20020a170902a38600b001d91cb5558bmr2904229pla.15.1707924699127; Wed, 14 Feb 2024 07:31:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707924699; cv=pass; d=google.com; s=arc-20160816; b=nhEBZdNT4DY5O89ccIuIOUtKKKbkZNDReBZkLQVILvbni1OXsQrxCGeXWQjwJBKALk vxnGRYmWOCGz03jKH7OREhVhbGr/fULNpDkaqeHaqHZmtjNp8feC126XXHQwv2GazHjl j8At4774T92IwSPOSZ91bKO64qTK3yZFXBeJmjtmm3+1YXSToNd4QMNdF5LcivM1czdY PtpCdvk793G0WgezP649UZ00XuE6M5fxnAHoHOYhJK2wTQTeeF7Ooe99iJpsI9KYQp3w 8Z/8P55f5BW4VZbIj67JN6bXZKn3ft/3UD467H/h3SKRicvmo1SCZzTUtF6hi+gLRYMP 0+yQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=y5sNZFr8z9VEp4WVx32gOwAtbq3+vupIHe5X9JYQqqc=; fh=Idu+ncVvN6ZGI+K4S5pNrFhbmvGP75Wx+d3YXm130bs=; b=z6+j82VDEPrZ5u5q8IUIO4SiYkHf5m4Oo4Ax/hxjbMDp5NqbciE1mhmNal3VfQabQA l4265FNW3n2dEdngpoU3xlt2doKWEBb7gwND4RHDTPvjihS7bJTbVJv1yDklqqdRnnG9 h1i53/1vPozITf1zn6kHJKJ54meriNJaZQ7QNuwDQ8LERauS8JGR8xi20X/Pha11lsC7 lTIeFdd94xhoEoVQwJ5wKliquUY2KuQkoy1p7gNoUGJM2Fc9HW/fAB8m/GZSCN94ZSkR 1/icO+33ortP+T8iwV4xG440+tqx7JjaukHuIqymjjoSyAR8fZtGNYcXyDtHyb0MaTq2 A1nQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=GSJ759Xu; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-65394-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65394-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=2; AJvYcCWjTKaMvB8d+HiInH49xROJ+SiLXUvR0aLZH5gkaNCfsKeWV5QU5Tcy18dNbkuJmtcERKrxLji0d7sYNYGr+4qCxvwODA== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id c11-20020a170902d48b00b001d8a56d4e92si3817360plg.124.2024.02.14.07.31.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 07:31:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65394-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=GSJ759Xu; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-65394-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65394-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 88511B28E29 for ; Wed, 14 Feb 2024 15:13:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B69805F87B; Wed, 14 Feb 2024 15:11:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="GSJ759Xu" Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 884DF5C601 for ; Wed, 14 Feb 2024 15:11:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707923515; cv=none; b=KB18qPlqqWIasPnB6iqhtA5snXo/RiGOtFkXA5Kjfn0Rb26Pn8AmlrgjtEjh0ZwQv83+kTpu4UTK9/6dPBbi85D9ZPg7fpk7g8HuW6KhK5iI2dqc3xslKfoAqJBfyNS7Ov0nacnXUB+xEzXY08Tqjti2jEpXlYZhB0Bc7yc8BeM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707923515; c=relaxed/simple; bh=VWiq2FOmAhYjvTFVuJsVVTmsLD+Tnb2HxXwGKlfTSIY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CpyNiPCFxWVKOGKZz12FUGLkozsdmqvmidxGZqDl/6i5K7SV5P45P6IBWC/FU4yP/RPrkM1aDBUy9lj/fdEZx0pr8Q7ztgsFQxWegRda0Thx6L88FYka2OJzUZXsLygLPPTGPL2uCKL0q7E7PDRZBgoWADpOGZ5BAR4zJQDSqOw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=GSJ759Xu; arc=none smtp.client-ip=209.85.215.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-5d8b519e438so1727383a12.1 for ; Wed, 14 Feb 2024 07:11:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707923512; x=1708528312; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=y5sNZFr8z9VEp4WVx32gOwAtbq3+vupIHe5X9JYQqqc=; b=GSJ759Xuuu6QZ4HdikGvNl4BJtJs5PESdAGsug6L2D8gL75tuqxThERuVlw3jv4S6V hSAAv1UoCZzfC5NXqhgQtpbjHUqfv8MkYtfB4n3mSHjU+LmC8bEZipnKcSpjslsnsEQ8 e9vnOEitzZ1awpFWq4Ib4MMTDSsbJUo6tPLAM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707923512; x=1708528312; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=y5sNZFr8z9VEp4WVx32gOwAtbq3+vupIHe5X9JYQqqc=; b=nWz5sZnt2mXUT9CgBgJNFaWS/OthKIYQ+SPOxiNtt75Kzl8X6k7rrSsgCEGh+Z9Puq S+39p6NTXc3/jiuDfW+pizHaMlodV9xCRpJr/3nCJmCVnbF90vCqZQnq4EVjr776SnQy PcLT/+lhsphKWek4y31MYmFPxdPwPQxkTHyNXcwjWoaahq6FfeUNsBvPT9i7CHMiRqq8 dPi+fdJnMkju/86HMKHjnoqMdM7E9dkbpQTEElL92cUOwtBo8yu2A5CSqsM8WccytqHw EZrTkzmFFEs2EgKOmrn7l8vZAHuFUj8JpTt5DIhD+ZoqiKy20wu4mqpYeY7JWzT7g8p3 R/WQ== X-Forwarded-Encrypted: i=1; AJvYcCWci4vvpchSX5PsaAqBk66zQyrgGWjk8PUSc6a8uYz8jZQ0iuWS1j01Y0ELDkSkNYl/ESTWEV+pIbJuB2PMaVM6zbbyq2umO4QL6tBQ X-Gm-Message-State: AOJu0YyXmMeUJMkjEHCzIzAMNgtgYw/jLbNZhtyMmiX1fuptF0jGhDM8 rqOLzie9xL+D6u0nXXXD6n9ATFU4c+A0VWX2STjXKr0hlueIOXX3jM/pMrK0Og== X-Received: by 2002:a17:90b:4b8e:b0:298:bc6a:784f with SMTP id lr14-20020a17090b4b8e00b00298bc6a784fmr2863835pjb.20.1707923511687; Wed, 14 Feb 2024 07:11:51 -0800 (PST) Received: from localhost (56.72.82.34.bc.googleusercontent.com. [34.82.72.56]) by smtp.gmail.com with UTF8SMTPSA id sc7-20020a17090b510700b00296fcb4e668sm1524496pjb.25.2024.02.14.07.11.51 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 14 Feb 2024 07:11:51 -0800 (PST) From: jeffxu@chromium.org To: akpm@linux-foundation.org, keescook@chromium.org, jannh@google.com, sroettger@google.com, willy@infradead.org, gregkh@linuxfoundation.org, torvalds@linux-foundation.org, usama.anjum@collabora.com, corbet@lwn.net, Liam.Howlett@oracle.com, surenb@google.com, merimus@google.com, rdunlap@infradead.org Cc: jeffxu@google.com, jorgelo@chromium.org, groeck@chromium.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, pedro.falcato@gmail.com, dave.hansen@intel.com, linux-hardening@vger.kernel.org, deraadt@openbsd.org, =David.Laight@aculab.com, Jeff Xu Subject: [PATCH v9 5/5] selftest mm/mseal read-only elf memory segment Date: Wed, 14 Feb 2024 15:11:29 +0000 Message-ID: <20240214151130.616240-6-jeffxu@chromium.org> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog In-Reply-To: <20240214151130.616240-1-jeffxu@chromium.org> References: <20240214151130.616240-1-jeffxu@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790888848954263825 X-GMAIL-MSGID: 1790888848954263825 From: Jeff Xu Sealing read-only of elf mapping so it can't be changed by mprotect. Signed-off-by: Jeff Xu --- tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/seal_elf.c | 183 ++++++++++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 tools/testing/selftests/mm/seal_elf.c diff --git a/tools/testing/selftests/mm/.gitignore b/tools/testing/selftests/mm/.gitignore index 76474c51c786..eff280b17a6e 100644 --- a/tools/testing/selftests/mm/.gitignore +++ b/tools/testing/selftests/mm/.gitignore @@ -47,3 +47,4 @@ mkdirty va_high_addr_switch hugetlb_fault_after_madv mseal_test +seal_elf diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile index ba36a5c2b1fc..a0a12626cd19 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -60,6 +60,7 @@ TEST_GEN_FILES += mrelease_test TEST_GEN_FILES += mremap_dontunmap TEST_GEN_FILES += mremap_test TEST_GEN_FILES += mseal_test +TEST_GEN_FILES += seal_elf TEST_GEN_FILES += on-fault-limit TEST_GEN_FILES += pagemap_ioctl TEST_GEN_FILES += thuge-gen diff --git a/tools/testing/selftests/mm/seal_elf.c b/tools/testing/selftests/mm/seal_elf.c new file mode 100644 index 000000000000..61a2f1c94e02 --- /dev/null +++ b/tools/testing/selftests/mm/seal_elf.c @@ -0,0 +1,183 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include "../kselftest.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * need those definition for manually build using gcc. + * gcc -I ../../../../usr/include -DDEBUG -O3 -DDEBUG -O3 seal_elf.c -o seal_elf + */ +#define FAIL_TEST_IF_FALSE(c) do {\ + if (!(c)) {\ + ksft_test_result_fail("%s, line:%d\n", __func__, __LINE__);\ + goto test_end;\ + } \ + } \ + while (0) + +#define SKIP_TEST_IF_FALSE(c) do {\ + if (!(c)) {\ + ksft_test_result_skip("%s, line:%d\n", __func__, __LINE__);\ + goto test_end;\ + } \ + } \ + while (0) + + +#define TEST_END_CHECK() {\ + ksft_test_result_pass("%s\n", __func__);\ + return;\ +test_end:\ + return;\ +} + +#ifndef u64 +#define u64 unsigned long long +#endif + +/* + * define sys_xyx to call syscall directly. + */ +static int sys_mseal(void *start, size_t len) +{ + int sret; + + errno = 0; + sret = syscall(__NR_mseal, start, len, 0); + return sret; +} + +static void *sys_mmap(void *addr, unsigned long len, unsigned long prot, + unsigned long flags, unsigned long fd, unsigned long offset) +{ + void *sret; + + errno = 0; + sret = (void *) syscall(__NR_mmap, addr, len, prot, + flags, fd, offset); + return sret; +} + +inline int sys_mprotect(void *ptr, size_t size, unsigned long prot) +{ + int sret; + + errno = 0; + sret = syscall(__NR_mprotect, ptr, size, prot); + return sret; +} + +static bool seal_support(void) +{ + int ret; + void *ptr; + unsigned long page_size = getpagesize(); + + ptr = sys_mmap(NULL, page_size, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + if (ptr == (void *) -1) + return false; + + ret = sys_mseal(ptr, page_size); + if (ret < 0) + return false; + + return true; +} + +const char somestr[4096] = {"READONLY"}; + +static void test_seal_elf(void) +{ + int ret; + FILE *maps; + char line[512]; + int size = 0; + uintptr_t addr_start, addr_end; + char prot[5]; + char filename[256]; + unsigned long page_size = getpagesize(); + unsigned long long ptr = (unsigned long long) somestr; + char *somestr2 = (char *)somestr; + + /* + * Modify the protection of readonly somestr + */ + if (((unsigned long long)ptr % page_size) != 0) + ptr = (unsigned long long)ptr & ~(page_size - 1); + + ksft_print_msg("somestr = %s\n", somestr); + ksft_print_msg("change protection to rw\n"); + ret = sys_mprotect((void *)ptr, page_size, PROT_READ|PROT_WRITE); + FAIL_TEST_IF_FALSE(!ret); + *somestr2 = 'A'; + ksft_print_msg("somestr is modified to: %s\n", somestr); + ret = sys_mprotect((void *)ptr, page_size, PROT_READ); + FAIL_TEST_IF_FALSE(!ret); + + maps = fopen("/proc/self/maps", "r"); + FAIL_TEST_IF_FALSE(maps); + + /* + * apply sealing to elf binary + */ + while (fgets(line, sizeof(line), maps)) { + if (sscanf(line, "%lx-%lx %4s %*x %*x:%*x %*u %255[^\n]", + &addr_start, &addr_end, &prot, &filename) == 4) { + if (strlen(filename)) { + /* + * seal the mapping if read only. + */ + if (strstr(prot, "r-")) { + ret = sys_mseal((void *)addr_start, addr_end - addr_start); + FAIL_TEST_IF_FALSE(!ret); + ksft_print_msg("sealed: %lx-%lx %s %s\n", + addr_start, addr_end, prot, filename); + if ((uintptr_t) somestr >= addr_start && + (uintptr_t) somestr <= addr_end) + ksft_print_msg("mapping for somestr found\n"); + } + } + } + } + fclose(maps); + + ret = sys_mprotect((void *)ptr, page_size, PROT_READ | PROT_WRITE); + FAIL_TEST_IF_FALSE(ret < 0); + ksft_print_msg("somestr is sealed, mprotect is rejected\n"); + + TEST_END_CHECK(); +} + +int main(int argc, char **argv) +{ + bool test_seal = seal_support(); + + ksft_print_header(); + ksft_print_msg("pid=%d\n", getpid()); + + if (!test_seal) + ksft_exit_skip("sealing not supported, check CONFIG_64BIT\n"); + + ksft_set_plan(1); + + test_seal_elf(); + + ksft_finished(); + return 0; +}