From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (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 EFCDA43E06D for ; Tue, 11 Aug 2026 11:22:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786447370; cv=none; b=Za+T71uoKB8iGCk+EsQ6TYkFWb3u9Iph1Dird5j1BAt+PmNTJBLBTB9HBxm4OI/TkW7PEAHnajjeH92DiOy+Qj2Seayr1MN/3g9w/z1nZP7nVdksNW9+imXGz+zNuOoVYVeMARgXE2IvwvczJInJDOt4r3AcfdB4TtKxfwNmkMA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786447370; c=relaxed/simple; bh=8DQh2SWbpdhxbKfSWZUmdp9rHTYS6PAT02UU8JSnJKc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MKOK+zK+SrCQF3ssNBiGhq9GU42xBoCyNWBJej18uak0qxEGZndmOqUXnnloZMC/K06/JFa4JYuMTuIfpy/1uRHm6vIrpaPurilHbbvTnDHzeHBsAHPHpptviJTBzgmqekPqQK3sRu/hxX/68msdvBPliFcjNqdf8MQ/Gu/kzHQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gFnN6uck; arc=none smtp.client-ip=209.85.128.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gFnN6uck" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-49978908b35so3265385e9.0 for ; Tue, 11 Aug 2026 04:22:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786447363; x=1787052163; 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:content-type; bh=Rcy5zMa/mQvTQ0drm+XsfTSCaDYlJAty3EgPpWyKkzc=; b=gFnN6uckci5jXJJHFeT5sX3RYH2D9Bgq5YPhKUqgsP0SbmHqGMQjVGQdspm/YSAoHo Zen9iKAF4OTPUN36H14SvcYoCjKZC9b/BqKMQE1vx7CSEJRUpOqSeljErhJBD1HqTqgv OLvkCP3NihDjZod0jYgxpYtInCG6u4WumjZcbIcS8izQRs2RuOCoOYI4/wm0VXxx5gC9 zkp5EyS7+F8WE7FMDtDIyWu4V1QhTqLlpMBU4mR/iE6/+QcJ+Io1KKfU/iE4DJkXhXzq XjYwgQQ1IvcG+uyhPXpQj86fBF68MKW5MllxzW92VgBqx5lXmA25f/T0s1qJVI9GeyLP NZEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786447363; x=1787052163; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Rcy5zMa/mQvTQ0drm+XsfTSCaDYlJAty3EgPpWyKkzc=; b=PzW83SksYD7tbBUqgEiiysgHvDdgCfD7eocAwa2ur6d3YNoybjYmYVhiPpuY9FWE1F xyqXg/rBqvymZ07HjHKaaBXfcg6ire+YeJ+dLkIXRSKcvjDFpqtOGPDD2oPEnyRj86JJ RIIftqqu6ntHzgykEDS2cNT+f26XmbJ1yO6/3xiqRfWK0Jsov3IRQzFhWUiatXx5g+hk T5vtJ19Yw22I6CA3vOEBBUD9XnMD5sJRRloxf9nQHGqfyGP9Oxxmq8ySgXvyFtKuM+2Y /8LJIq+bdhDcF6td3a2CIJnT2uWSYx/vjn162gGYeOGraXEuEBAQV5a6uCPrY46vdVQP COag== X-Gm-Message-State: AOJu0YwhoD0QhR+99gqjmO0jzlk5cVON1S3bzP4YjqTlXoexjsT2q4IV +DvoZUXhM1FbllGhGvfH7hjxTXMonneMhyWIgGhmDHj643MeBj08Vu3V X-Gm-Gg: AR+sD108s5i/EzcFL94eG/ccyZyohNW5Zx7Ku/0sLY5dN/tTTkdqC8Gu+ALQA6I+Kng Fc9VS1yhz1GkBOmZspcF5XLu6zRDn5SxxMAIjg3gKut7tD0/seN1vH9T5O5ODx3D2N1FQWHxebK /n9Y9nHxensGqye62gbSgzSV1YjG7waMin4+oZdr3CaQ/wFYAl5HbCNQ92wo5SflxBeJoVZa1QM WOoY0+2T/AlSnJ8wxqR/020K4zIIircT2CaJ5UWYAQHfBNoJNGxXRGTTodR95SJ9yjI9ol1IGEn iizqKubGOpkd30CheJp0U6cgiknS6LqgwU5FZ4AaOY9gkndqjjMb4WVbzLJnElKfSdmwjtnBTbP 5MrxEuJYyIASHoz5mxqbFU4P1+C0WSIrtZtqURq/9ycz+UQ5IScBhzyfXBD8kBz6jylTqWTq9AG KK6Yq19r0i0zuqqy7JiOXHuLyRm6P4aOMdVftNVhFgrLN8JQNUVOoFQYJiWbcBc85l/eS5HlYN9 Otz0cKkNcAqJkk= X-Received: by 2002:a05:600c:1912:b0:499:737c:8a8b with SMTP id 5b1f17b1804b1-4997845e778mr44282705e9.12.1786447363236; Tue, 11 Aug 2026 04:22:43 -0700 (PDT) Received: from dell-desktop ([2a02:587:4b5e:1100:7303:647d:7cc4:8552]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4997889c2bcsm47547665e9.3.2026.08.11.04.22.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Aug 2026 04:22:42 -0700 (PDT) From: Anastasios Papagiannis To: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, david@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com, kpsingh@kernel.org, matt@bobrowski.net, song@kernel.org, tasos.papagiannnis@gmail.com Subject: [PATCH bpf-next 2/5] bpf: Add user memory access kfuncs for mm_struct Date: Tue, 11 Aug 2026 14:21:51 +0300 Message-ID: <20260811112154.94053-3-tasos.papagiannnis@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260811112154.94053-1-tasos.papagiannnis@gmail.com> References: <20260811112154.94053-1-tasos.papagiannnis@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit When security_bprm_check runs, the arg and env strings for the exec have been copied into bprm->mm. The new address space has not been associated yet with a task_struct until exec_mmap(), so existing BPF user memory helpers can only read from the calling task's old address space. Add bpf_copy_from_user_mm() and bpf_copy_from_user_mm_str() kfuncs. Both take a struct mm_struct pointer directly, allowing callers to access trusted address spaces that are not associated with a task_struct. bpf_copy_from_user_mm() has similar semantics to bpf_copy_from_user_task(). bpf_copy_from_user_mm_str() copies one NUL-terminated string and returns its size including the NUL terminator. It accepts BPF_F_PAD_ZEROS to clear unused destination bytes on success. Register both kfuncs with the common BPF_EVENTS kfunc set and mark them KF_SLEEPABLE because accessing the remote address space can fault. The next patch marks linux_binprm->mm as trusted-or-null, allowing BPF LSM programs attached to security_bprm_check to read arguments beginning at bprm->p after a NULL check and reject an exec based on its command-line arguments. Signed-off-by: Anastasios Papagiannis --- kernel/bpf/helpers.c | 2 + kernel/trace/bpf_trace.c | 83 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 6388b6b23e49..9ba3362832cd 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -4931,6 +4931,8 @@ BTF_ID_FLAGS(func, bpf_iter_kmem_cache_destroy, KF_ITER_DESTROY | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_local_irq_save) BTF_ID_FLAGS(func, bpf_local_irq_restore) #ifdef CONFIG_BPF_EVENTS +BTF_ID_FLAGS(func, bpf_copy_from_user_mm, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_copy_from_user_mm_str, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_probe_read_user_dynptr) BTF_ID_FLAGS(func, bpf_probe_read_kernel_dynptr) BTF_ID_FLAGS(func, bpf_probe_read_user_str_dynptr) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 891897f8a1b3..8931b3b2fc9d 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -3619,6 +3620,88 @@ __bpf_kfunc int bpf_send_signal_task(struct task_struct *task, int sig, enum pid return bpf_send_signal_common(sig, type, task, value); } +/** + * bpf_copy_from_user_mm() - Copy data from an address space + * @dst: Destination address, in kernel space + * @dst__sz: Number of bytes to copy + * @unsafe_ptr__ign: Source address in the address space + * @mm: Address space to copy from + * @flags: Reserved for future use; must be zero + * + * Copies data from the user address space associated with @mm. The destination + * is zeroed if the requested number of bytes cannot be copied in full. + * + * Return: 0 on success, -EINVAL if @flags is non-zero, or -EFAULT if the copy + * fails or is partial. + */ +__bpf_kfunc int bpf_copy_from_user_mm(void *dst, u32 dst__sz, + const void __user *unsafe_ptr__ign, + struct mm_struct *mm, u64 flags) +{ + int ret; + + if (unlikely(flags)) + return -EINVAL; + + if (unlikely(!dst__sz)) + return 0; + + ret = access_remote_vm(mm, (unsigned long)unsafe_ptr__ign, + dst, dst__sz, 0); + if (ret == dst__sz) + return 0; + + memset(dst, 0, dst__sz); + return ret < 0 ? ret : -EFAULT; +} + +/** + * bpf_copy_from_user_mm_str() - Copy a string from an address space + * @dst: Destination address, in kernel space. This buffer must be + * at least @dst__sz bytes long + * @dst__sz: Maximum number of bytes to copy, including the trailing NUL + * @unsafe_ptr__ign: Source address in the address space + * @mm: Address space to copy from + * @flags: The only supported flag is BPF_F_PAD_ZEROS + * + * Copies a NUL-terminated string from the user address space associated with + * @mm. If the string is too long, @dst is still NUL-terminated unless @dst__sz + * is zero. + * + * If BPF_F_PAD_ZEROS is set, the unused portion of @dst is cleared on success + * and all of @dst is cleared on failure. + * + * Return: The number of copied bytes including the NUL terminator on success, + * or a negative error code on failure. + */ +__bpf_kfunc int bpf_copy_from_user_mm_str(void *dst, u32 dst__sz, + const void __user *unsafe_ptr__ign, + struct mm_struct *mm, u64 flags) +{ + int ret; + + if (unlikely(flags & ~BPF_F_PAD_ZEROS)) + return -EINVAL; + + if (unlikely(!dst__sz)) + return 0; + + ret = copy_remote_mm_str(mm, (unsigned long)unsafe_ptr__ign, + dst, dst__sz, 0); + if (ret < 0) + goto err; + + if (flags & BPF_F_PAD_ZEROS) + memset(dst + ret, 0, dst__sz - ret); + + return ret + 1; + +err: + if (flags & BPF_F_PAD_ZEROS) + memset(dst, 0, dst__sz); + return ret; +} + __bpf_kfunc int bpf_probe_read_user_dynptr(const struct bpf_dynptr *dptr, u64 off, u64 size, const void __user *unsafe_ptr__ign) { -- 2.55.0