Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp2077897lqo; Sun, 19 May 2024 12:13:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXfsm6jg2G6RoGBH5P7aR0vqpe25AN2E+fxeHUieQ8ZmnNcgekE4PuKrmVRZADe182WAQicIGTz+bfRLRgDDrnzSKxubFB7xRTqm0Xcpg== X-Google-Smtp-Source: AGHT+IGpnHluD/rxyr3SOw2otFyFa6g6TxmjAso1P91fOzgeXK/boMx/RlWapxqn6+fimEsvrCf6 X-Received: by 2002:a05:6a21:1693:b0:1af:f64c:b78c with SMTP id adf61e73a8af0-1aff64cba8fmr20486921637.0.1716145999273; Sun, 19 May 2024 12:13:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716145999; cv=pass; d=google.com; s=arc-20160816; b=qaVJOHjWICNtHAPyOO8Kl8ns65wGa/t6wOVRB5gc1A8HkUOwu0TaHeCd0aLbxvI7Wz CNGJgij3ZJKqKmun8S8Y+SRm1gdh+VZa22iLvUVVzviGla3XYSR7wddTGHLzdu5/aMM0 9+nTadQeuLjmXDOfoLAuy/60i8JIKEaBTZQJ7eluzU9OvCYgI0sQ27LTzgQbpQoihvqF x8JbveF5Rg4HEaAJupPIXfde6RucOplpGPjOI6QaV6fNV7iHFTaiji2EEk1GjQ7fo2M4 t1RkDCy4aT9E2TFFK49DhQ8q51eGHkXoOM0KlZpNTvi4shLoOZEzeztbIlhzNW+D3qGi CL2A== 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=m3OtVtNf5ShWXj1bE7269UiIDqXp6vnkrO47qSa2kQQ=; fh=k51OkzAFlQ0QYroNG0Et8nzuLNcA4GJlKC5XYFLtcdc=; b=R5LA5NlC0D4pcxIyuThOY+tzcxt9ms4afiO5iKO5JtjZMZzerAceY0U/K5HIB3/feH CAp48QErrL/aop9zOj9S3mbrWp7tXJYhbFLF8ylyTPKLz5tGFrakYs8ghYeTfHMAQJhp A2YYan/EjeoJhjmZZpQGpW8oann+WP+x4ORBo+r4QB3oKj5Xey686u991pznU2O8Ins1 iIsj5wIxjX9AMgeP6MIBR++q5rz0RQ3Xj0mEmICwLVb661Fig0W+LP5BmBT67YmuSSTt wDIWDqJNjKaLONA2YU3XGPxw+3941NTIDBSty/njFzIbvD9yIGuElwJnkEPbZ4lr3lP5 xK7A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=k57TD1iS; 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-183219-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-183219-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2bd80a23458si344275a91.101.2024.05.19.12.13.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 May 2024 12:13:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-183219-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=k57TD1iS; 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-183219-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-183219-linux.lists.archive=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 sv.mirrors.kernel.org (Postfix) with ESMTPS id DDE4D2811D0 for ; Sun, 19 May 2024 19:13:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B2D0071739; Sun, 19 May 2024 19:12:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="k57TD1iS" Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 046ADD2EE for ; Sun, 19 May 2024 19:12:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716145978; cv=none; b=BvPs1K50Tdq6mkwSZL0kXUQx2L8VV32I3Jgsx5knDX/F5+enfGNWKUw5VIXJR7UH2KmLB9gkiOfyUmDyrztQI9iMwvSCF3g+5v4ibRT+SAcIXxWVUhM6eVmJakh+EHOgEJTTpM2miAoH6pMrU+5ApZGnkYmL+6xhHjYgFy6N/C0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716145978; c=relaxed/simple; bh=wNgy2pm8z4ldp8MuPhDft19azofrU0oPWpTD6QjozZ8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UTUWamlVNfevdNKqRDUmUKeNHtVVzE2rzhZ6oCURcLiEA0vdoVqs/3NnvWPTxzTH5TPCEpDKPXgMdzPu182etDm6bROg0bcN1PVs1quiCBfq4XTP9QcVEUJURrs0QHM5g4vRqH41Sa6XyMPk+Dn95ZaX66yPY4m8V64xoZblNEY= 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=k57TD1iS; arc=none smtp.client-ip=209.85.214.171 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-pl1-f171.google.com with SMTP id d9443c01a7336-1f2f566a7c7so23310525ad.1 for ; Sun, 19 May 2024 12:12:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1716145976; x=1716750776; 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=m3OtVtNf5ShWXj1bE7269UiIDqXp6vnkrO47qSa2kQQ=; b=k57TD1iShxFnfC2pp8NYHPv0zxZ/Iwfy7yN1pt6fWUamOPDLQolO1/3FcnzUCzp3LB OtW3grJTjC++3uwKsKo5TOjM0S87Ir/Ot1ycwFSgUMYkfZ4Uvn5Fg5QAkl6f2/OW0D8T sjgNZyaEOEhN3J71zlhlhGAB7MnwpprIdWF7o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716145976; x=1716750776; 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=m3OtVtNf5ShWXj1bE7269UiIDqXp6vnkrO47qSa2kQQ=; b=gk89zDlkDRSE9dTwhC0uiZBYIGNwnzkQn/9Sf2KG40SpQyNXImCltONYbBAjU1DqwQ bH9IZMR/U4twxUrpEKPrwjyaRmqc83xlwECgTKdN2hVsh257v9KLfic7JegADr96Q1vy 0+NkYNVrmoqdWFs/HhCUT1bqQ+felZcuKCXqOJ3Zyx3ai6ix+wWT8jA5xphGs21wEFq/ gcTvUES4qNXyw8jQBX9h51qd7COnsH4oPLtg3AEVSQ2JXy8nIXvNwynaTD9CUv9qF8jZ W+Y8PjEceWf/+GgAb+7ZDvGCG96iE4ONqrfuAymKmXc5r/q96UzFjqQRqOw/V6Gx7AwJ NlFw== X-Forwarded-Encrypted: i=1; AJvYcCU3p2A1jG9js9eFm3bRuYyOwPDpTzUDCxZiJTZrbVCEgwGtcHXodx3pX0rT7msbp4j+jNRdnQsIy80uUdLrxDZssSJVFyEYrm5UROuF X-Gm-Message-State: AOJu0Yw1Mudyl28DPiFPZqyYyjwVFYihmgw0FFTFVb2c0BTf+7LKCXDy Y5GYmJ208gRuSUutdqE3zQX+cx3m/GoeqxpkmdAnPYoxhajbSn7PrVowMkZukQ== X-Received: by 2002:a17:903:2b07:b0:1f0:9aca:97b9 with SMTP id d9443c01a7336-1f09acaa30fmr121431235ad.68.1716145976075; Sun, 19 May 2024 12:12:56 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0b9d16f2sm188855635ad.41.2024.05.19.12.12.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 May 2024 12:12:55 -0700 (PDT) From: Kees Cook To: Mark Rutland Cc: Kees Cook , Vitor Massaru Iha , Brendan Higgins , David Gow , Rae Moar , "Gustavo A. R. Silva" , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-hardening@vger.kernel.org Subject: [PATCH 1/2] kunit: test: Add vm_mmap() allocation resource manager Date: Sun, 19 May 2024 12:12:52 -0700 Message-Id: <20240519191254.651865-1-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240519190422.work.715-kees@kernel.org> References: <20240519190422.work.715-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5613; i=keescook@chromium.org; h=from:subject; bh=wNgy2pm8z4ldp8MuPhDft19azofrU0oPWpTD6QjozZ8=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmSk81T50rcpAOSlXL8bkcrTI2RR0tm5BAlNK8R nb+OGPQ79WJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZkpPNQAKCRCJcvTf3G3A JjxFD/4iRIjfhVQa49n95Z2UAyf3e2Lfrswbx6Upw/fDRLSG4t8nNL3GIgdSQsX5z+KBR9b60V7 NHB11FzIrEz+3X/R1yyXhKOEqxuqUpYxFpZrKrMq+2nHE31wQCla7dm4bUr8ElF0Se3hhfH36Y2 V1tJGteFBLk9R92hEn585x+9T7x+ccGGxXe1NCiQAxEFVx/W+4pv2Z9AZqHz3g5nhpDOCd+w3Oz 4GgxYARWnJmm8++UZWpJnUMSjqT+1IgJiI/x9IpBPLTUhnFMxJ6lY+oNDyC4hAo65Onn8QHF5VV dpvwU/51JWdFElHHSE/O/BHJ18cmjrjn3/A8KB2NcPS0VO8kSIhdZdHPGkrWh5auZlKkAf5ts5h couc3iroXRwVk0eVeZ7U79OP6uSCwazjFIZwg4k05VMb96HlDy6jE3SAwRl1H3MT/XnrEhnTGjV C2o2MVd6BijHfJThjFmZAV3CozFe/JbrrLcqZf+How73xxhNFaJpUev+VYrLcJrI7FjLlpL4vqq aBfrxx9ubpcxw0cYZBMOWLR/K4J9+OdwsYsiNN6mmOBgLPRwmjXyqZZkPLi5dui0tA56L1T67Em 1tlDtSzaKYrxCATVeS6jZV43HFP6vXKMRVY1sIgsw82D/3iF1wOJBKpRbv+xxFSWIujmRB9Vrke wPLV3zAgTzm0YDg== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit For tests that need to allocate using vm_mmap() (e.g. usercopy and execve), provide the interface to have the allocation tracked by KUnit itself. This requires bringing up a placeholder userspace mm. This combines my earlier attempt at this with Mark Rutland's version[1]. Link: https://lore.kernel.org/lkml/20230321122514.1743889-2-mark.rutland@arm.com/ [1] Co-developed-by: Mark Rutland Signed-off-by: Mark Rutland Signed-off-by: Kees Cook --- include/kunit/test.h | 17 ++++++ lib/kunit/test.c | 139 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 155 insertions(+), 1 deletion(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index 61637ef32302..8c3835a6f282 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -478,6 +478,23 @@ static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO); } +/** + * kunit_vm_mmap() - Allocate KUnit-tracked vm_mmap() area + * @test: The test context object. + * @file: struct file pointer to map from, if any + * @addr: desired address, if any + * @len: how many bytes to allocate + * @prot: mmap PROT_* bits + * @flag: mmap flags + * @offset: offset into @file to start mapping from. + * + * See vm_mmap() for more information. + */ +unsigned long kunit_vm_mmap(struct kunit *test, struct file *file, + unsigned long addr, unsigned long len, + unsigned long prot, unsigned long flag, + unsigned long offset); + void kunit_cleanup(struct kunit *test); void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt, ...); diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 1d1475578515..09194dbffb63 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -11,13 +11,14 @@ #include #include #include +#include +#include #include #include #include #include #include #include -#include #include "debugfs.h" #include "device-impl.h" @@ -871,6 +872,142 @@ void kunit_kfree(struct kunit *test, const void *ptr) } EXPORT_SYMBOL_GPL(kunit_kfree); +struct kunit_vm_mmap_resource { + unsigned long addr; + size_t size; +}; + +/* vm_mmap() arguments */ +struct kunit_vm_mmap_params { + struct file *file; + unsigned long addr; + unsigned long len; + unsigned long prot; + unsigned long flag; + unsigned long offset; +}; + +/* + * Arbitrarily chosen user address for the base allocation. + */ +#define UBUF_ADDR_BASE SZ_2M + +/* Create and attach a new mm if it doesn't already exist. */ +static int kunit_attach_mm(void) +{ + struct vm_area_struct *vma; + struct mm_struct *mm; + + if (current->mm) + return 0; + + mm = mm_alloc(); + if (!mm) + return -ENOMEM; + + if (mmap_write_lock_killable(mm)) + goto out_free; + + /* Define the task size. */ + mm->task_size = TASK_SIZE; + + /* Prepare the base VMA. */ + vma = vm_area_alloc(mm); + if (!vma) + goto out_unlock; + + vma_set_anonymous(vma); + vma->vm_start = UBUF_ADDR_BASE; + vma->vm_end = UBUF_ADDR_BASE + PAGE_SIZE; + vm_flags_init(vma, VM_READ | VM_MAYREAD | VM_WRITE | VM_MAYWRITE); + vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); + + if (insert_vm_struct(mm, vma)) + goto out_free_vma; + + mmap_write_unlock(mm); + + /* Make sure we can allocate new VMAs. */ + arch_pick_mmap_layout(mm, ¤t->signal->rlim[RLIMIT_STACK]); + + /* Attach the mm. It will be cleaned up when the process dies. */ + kthread_use_mm(mm); + + return 0; + +out_free_vma: + vm_area_free(vma); +out_unlock: + mmap_write_unlock(mm); +out_free: + mmput(mm); + return -ENOMEM; +} + +static int kunit_vm_mmap_init(struct kunit_resource *res, void *context) +{ + struct kunit_vm_mmap_params *p = context; + struct kunit_vm_mmap_resource vres; + int ret; + + ret = kunit_attach_mm(); + if (ret) + return ret; + + vres.size = p->len; + vres.addr = vm_mmap(p->file, p->addr, p->len, p->prot, p->flag, p->offset); + if (!vres.addr) + return -ENOMEM; + res->data = kmemdup(&vres, sizeof(vres), GFP_KERNEL); + if (!res->data) { + vm_munmap(vres.addr, vres.size); + return -ENOMEM; + } + + return 0; +} + +static void kunit_vm_mmap_free(struct kunit_resource *res) +{ + struct kunit_vm_mmap_resource *vres = res->data; + + /* + * Since this is executed from the test monitoring process, + * the test's mm has already been torn down. We don't need + * to run vm_munmap(vres->addr, vres->size), only clean up + * the vres. + */ + + kfree(vres); + res->data = NULL; +} + +unsigned long kunit_vm_mmap(struct kunit *test, struct file *file, + unsigned long addr, unsigned long len, + unsigned long prot, unsigned long flag, + unsigned long offset) +{ + struct kunit_vm_mmap_params params = { + .file = file, + .addr = addr, + .len = len, + .prot = prot, + .flag = flag, + .offset = offset, + }; + struct kunit_vm_mmap_resource *vres; + + vres = kunit_alloc_resource(test, + kunit_vm_mmap_init, + kunit_vm_mmap_free, + GFP_KERNEL, + ¶ms); + if (vres) + return vres->addr; + return 0; +} +EXPORT_SYMBOL_GPL(kunit_vm_mmap); + void kunit_cleanup(struct kunit *test) { struct kunit_resource *res; -- 2.34.1