From patchwork Mon Apr 15 16:35:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13630311 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id ACC70C4345F for ; Mon, 15 Apr 2024 16:35:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DB8FE6B0098; Mon, 15 Apr 2024 12:35:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D176B6B0099; Mon, 15 Apr 2024 12:35:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B6B2C6B009B; Mon, 15 Apr 2024 12:35:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 8BF686B0098 for ; Mon, 15 Apr 2024 12:35:44 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 5F5FFA0675 for ; Mon, 15 Apr 2024 16:35:44 +0000 (UTC) X-FDA: 82012317408.23.71FEBEB Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf18.hostedemail.com (Postfix) with ESMTP id 9752D1C000D for ; Mon, 15 Apr 2024 16:35:42 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=ltysLs5h; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf18.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.181 as permitted sender) smtp.mailfrom=jeffxu@chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713198942; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=AJHOXE7W4iXMxi8stv8aQLFD0obJISw2P5aysbaDa0s=; b=V4KL8aM5+MCu8F6wMeUgFi1ybLtbHAxXZOU9tMfMgebrZOCCZ4iwZcVvIf0vtmheS58TiI WMRzu0Kd0jDWxcDR8m0aCJoGQalvhefvvA5dVzNx/zhR7JMWoU3TnDL+YSvStzUM2CcRRM GEtFBFULn6d6jL+9OwZlDtVNvj5dnk4= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=ltysLs5h; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf18.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.181 as permitted sender) smtp.mailfrom=jeffxu@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713198942; a=rsa-sha256; cv=none; b=GXODd41Pat7awqEaKbWOQ5P3vK/7sY5c6PdugLr73CeDXgLAq4t8gIpsXrOW9OOcYK72K7 YeNRAjJtI/Xhflz6GueDBtK7Mn//OMF/fNsU8bghCvP0ZSNIcQ2KioW71ybSz707jl2Ffa 43lFJFUJF9/TiIybPQHfbVJKxVfRDeI= Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-6eff2be3b33so1151229b3a.2 for ; Mon, 15 Apr 2024 09:35:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1713198941; x=1713803741; darn=kvack.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=AJHOXE7W4iXMxi8stv8aQLFD0obJISw2P5aysbaDa0s=; b=ltysLs5hep2x2s9diIuVEpH0tQMQLPWewRPXn9brR4D5gu8plTS0AUj0N1BcRhPDJz nn1ATY/QSumwbTdEGFfWO0vKfu/opgud32TcZROCHBijcWPHrNoEdNu19zUjmlqwnHGx X7kOip3WKF5lUnW5tcUpYwi80h/XOBnH1O5ro= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713198941; x=1713803741; 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=AJHOXE7W4iXMxi8stv8aQLFD0obJISw2P5aysbaDa0s=; b=sTsKtdCGwNR5TNJylQXjiQ4a0K1841L4SOxPwkMqL2lrdZKUYDy4vpW9CMHDDos8Zf QQQst2Nb0Q2WaF4MiBsUjCXvJGQj0OCG/Q5TEBYXuYtXLgATicVvkLZml6RJEl9gIIyn DR+XYMJs0KKmfDy8yLUzF9of/i1JmJ6jO7Cy2Ecoo4PajYubfjPjZ8aDolovVGcoan7t bqsIofhUGChqxXYr9DVZghgqgORRp91zu5Kq3pr67RE9PNyRMtHvujpc8Jxef5HRkLwK VvdbUaFUW+H71ieQLhrMOZDRW5Pl3tw9Jwxoa0hvD70C2o9qjPlApjvdqEUgAQcNXmEq O+Bg== X-Forwarded-Encrypted: i=1; AJvYcCXuDNlXLjGdRFaGHWf78lN5nuqDBlItGYeYfLKHjqNGbXvaQsjbFSi6uabt8e7DBL6EjXnbAuahRAiCtEYcZ3L2sdc= X-Gm-Message-State: AOJu0YwQwLuw8VARzbmmAohVnWGANw+lmhV7JkQ1tUFWN6zkIBNP0eny PyP/EvPHLdsew0dOftfGlv8/IeJ3lPTFxzgtBJgj92bwC/9zP6ojcXA/YBT9uQ== X-Google-Smtp-Source: AGHT+IGpxBiXitUV65X12uO9kCt9yTS7pqi2G5lLyGKRXo70DVpYj0l9hUD2hNlk3bYPsgQK0YTsXQ== X-Received: by 2002:a05:6a21:151a:b0:1a9:b3e9:a62c with SMTP id nq26-20020a056a21151a00b001a9b3e9a62cmr8978922pzb.48.1713198941478; Mon, 15 Apr 2024 09:35:41 -0700 (PDT) Received: from localhost (15.4.198.104.bc.googleusercontent.com. [104.198.4.15]) by smtp.gmail.com with UTF8SMTPSA id b13-20020a630c0d000000b005dc4da2121fsm7213902pgl.6.2024.04.15.09.35.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 15 Apr 2024 09:35:40 -0700 (PDT) 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, Jeff Xu Subject: [PATCH v10 5/5] selftest mm/mseal read-only elf memory segment Date: Mon, 15 Apr 2024 16:35:24 +0000 Message-ID: <20240415163527.626541-6-jeffxu@chromium.org> X-Mailer: git-send-email 2.44.0.683.g7961c838ac-goog In-Reply-To: <20240415163527.626541-1-jeffxu@chromium.org> References: <20240415163527.626541-1-jeffxu@chromium.org> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 9752D1C000D X-Stat-Signature: ckr3oztesmmsermu39np151gzizngmjg X-Rspam-User: X-HE-Tag: 1713198942-94568 X-HE-Meta: U2FsdGVkX1+z3Bg9EItLbSGKaskUsAwKEznPFQgVL+D1fw8cdJLFuN/Jnmu9wdPXwVi199A6pPRNu2FIz0Co7y6j4+ZrdWB8zNHetZwyFfgabbmhmU6MrJaPwT05oNmS2d5530YzLemZCYjNaxsLKqmOyoyeE70lmVkgcaHhNFfSW2y2Fn6GM+ZPOKreikv7f5zLBVyC6s/3PwUTrtVNWTsAQKZ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1/NQBnWNMnY7bdc9RDsN4nyiX0QYsT11P98P83wuXSIrk6Q2RLQeLl8SE/RmH8nn5odmYz/VSm28epsa+nIt9bTgsMPN4zpB7BVKojgRkY9NQP0+wdUXapg6fq6JrYnNhnC+l5nApA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 98eaa4590f11..0b9ab987601c 100644 --- a/tools/testing/selftests/mm/.gitignore +++ b/tools/testing/selftests/mm/.gitignore @@ -48,3 +48,4 @@ va_high_addr_switch hugetlb_fault_after_madv hugetlb_madv_vs_map mseal_test +seal_elf diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile index 95d10fe1b3c1..02392c426759 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; +}