From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (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 29F073E00A0 for ; Mon, 31 Aug 2026 09:23:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788168226; cv=none; b=IDY2xilRLQukUNI3ljuhcs3UBdYWvfhCWBsiH5geHw7a/S7YoY+riQhdyrBHD3lqZwVKLnmUYAmoflsOXwy4CYc171WxO1Vu8okifrmSUAwhzQ8xq8p/2UmCtsNm9GKxxgtjr8V53YOa5fkbkCLFkkJBxQjQGVJxqHL3GO3tFQc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788168226; c=relaxed/simple; bh=xspFFxFjYAqvjceAxqadMuKixodXwHOZDcpMBk8R9S8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XXPsy5iL6YcR2v5Oi/+LlghbwLbmcwWbRIqTug7hs5V5v/5pXP8pbMbGKa3GSei+2J5DXGqRxOSWUVXDbbG61s4NlTaQML8ULQ9+YEz+mQplyVzcdP5GWvq8GuFA5Y0sc37ufCP9PkaSdmWth/yrBkzBS5H7/uENTijxy8+Feuk= 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=IDJgWm9/; arc=none smtp.client-ip=209.85.221.47 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="IDJgWm9/" Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-48431648f33so1044605f8f.0 for ; Mon, 31 Aug 2026 02:23:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788168221; x=1788773021; 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=Gy3RmBcuruQaR7WrIBsKEmLLWdsVsMuBKWJEzmGMhsc=; b=IDJgWm9/RTCQSYlJnx3AujjuI/uDDWAx0223c4yNs/c1oVD1SmQfWxykVZAcPFhzG3 9kVT03GvpHWIdiUp/gw9nI6BNFzN4KjxI7cb8pTOLyjEz8uVrCfrQGCwfWrgcdkqA1Cg 4g1bTnJn1vwHmKhU+Br9Xk/EoL2NdVCEjJQeDjKX4uVcCJHnHQFNigYLNijEwQz9nFTG pn7GCoUFxnvPj+RYQmK9FVD2j+LaCJBUGVbQk2jKFOOIIZD2XSomPnnfce6jZ6EZnwj8 AZadTKZc+7u2NJbdXC+VooaXPW0qxlyHUsULCDhXSZzzk8SnSglEr+QLGO6pb1eS3rgI d+Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788168221; x=1788773021; 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=Gy3RmBcuruQaR7WrIBsKEmLLWdsVsMuBKWJEzmGMhsc=; b=FVKsr1Od4kMjsA302dHraMkOZptx2dh0s4VMUarWnuyNJ0EocGnGizT0rXdTXXgtbN MoHUZgot6sFo6fag0dUfhGfl1roIFaVb58AYiePdUO0BniKqJVnPKNxCrWO29fyJrnvo F+Ezzf+qu8WkUq1x5q+3SOzDtj1JNyoJ66O+1cJxyy97z2wr1+EW9KGYy7/qRY1xtPnW gZ1idjHsWHGPg06M5xJmvLxcbqDoio6kK2/HeQgr9VwWnqDlELMT2UyvwkYdZs2XmYxU RlTIZkqUIo4NokwD5kgpZiRZ8nYLHIY6KrUJBxEHLsGSiDcJHWGzVmtDTiULxNotb1yt l/fQ== X-Gm-Message-State: AFuF++lR4Vpc020z6jP0NOoxv23Xeeu0hVthL3U9F51muvq0Pfvt1tEC 1J8/9jXdbUkmJru5xz6d4hDztv60X7x7C1XS8jMmLLoDCatf01bzES4/ X-Gm-Gg: AR+sD11nEklPnllcMtG7AdVlll14jPaMONLHhE011zu0dbE1NZAfvlKkHqoWCgjf49a JD+tGjoeHMZF3O9RDurytz909XTEzpNCn5HqfliFo9hQKDOyPNAhrbAxz5qUBwJdNSMEtGisRTz jUIvWApM17iWBegZRKNCoFRWloHhC9Jn9a0NpQvegcvPhd7MrkbRwVESwQV/ofekv4ZyYfup8gR /DrLflHbcDAsSImRVPpoe9aQ+whMVDuaF6Zb/kJcMPKX03SYlNLw2/+/Yf2lRdpVL7zfKro9+A3 bnwcG6AsBf8pBYppwIBh3PWpwjZoJkysur2cXqHFnibHmrzrtd6Gz/MR8WwiSzU2d/sPPFmCeUd nksf1UqM4Nj7N/eb3xXQIjHlblp/hsVnViXBbDTQcNcSkPE5K4hx8L2fHLGf+y1BMXjC1bkv3yA TZd8wdSpyuTbz99SF9C78Fa2HseQ7SPG1xy8RHj866W+H5wtQT3h94N0NN4Y90rTh6eIVDawyMy g== X-Received: by 2002:a05:600c:4f49:b0:495:5d6d:9cc1 with SMTP id 5b1f17b1804b1-49cd52b2c93mr119072045e9.0.1788168221065; Mon, 31 Aug 2026 02:23:41 -0700 (PDT) Received: from dell-desktop ([2a02:587:4b5f:900:ef25:2f4:1792:4e6f]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49b9500c80asm318248155e9.9.2026.08.31.02.23.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Aug 2026 02:23:40 -0700 (PDT) From: Anastasios Papagiannis To: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, viro@zeniv.linux.org.uk, brauner@kernel.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, utilityemal77@gmail.com, tasos.papagiannnis@gmail.com Subject: [PATCH bpf-next v3 1/7] mm: Add copy_remote_mm_str() Date: Mon, 31 Aug 2026 12:22:59 +0300 Message-ID: <20260831092305.42062-2-tasos.papagiannnis@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260831092305.42062-1-tasos.papagiannnis@gmail.com> References: <20260831092305.42062-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 copy_remote_vm_str() gets the target address space from a struct task_struct. This does not work for an address space that exists but is not yet associated with a task_struct, such as the mm held by struct linux_binprm during exec. Add copy_remote_mm_str(), which operates directly on a struct mm_struct. Signed-off-by: Anastasios Papagiannis --- include/linux/mm.h | 2 ++ mm/memory.c | 33 +++++++++++++++++++++++++++++---- mm/nommu.c | 33 +++++++++++++++++++++++++++++---- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 485df9c2dbdd..eede435bf4a3 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3222,6 +3222,8 @@ extern int access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf, int len, unsigned int gup_flags); #ifdef CONFIG_BPF_SYSCALL +extern int copy_remote_mm_str(struct mm_struct *mm, unsigned long addr, + void *buf, int len, unsigned int gup_flags); extern int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr, void *buf, int len, unsigned int gup_flags); #endif diff --git a/mm/memory.c b/mm/memory.c index 6b8280cfc1db..4c5f0b629889 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -7141,12 +7141,15 @@ EXPORT_SYMBOL_GPL(access_process_vm); * Copy a string from another process's address space as given in mm. * If there is any error return -EFAULT. */ -static int __copy_remote_vm_str(struct mm_struct *mm, unsigned long addr, +static int __copy_remote_mm_str(struct mm_struct *mm, unsigned long addr, void *buf, int len, unsigned int gup_flags) { void *old_buf = buf; int err = 0; + if (unlikely(len == 0)) + return 0; + *(char *)buf = '\0'; if (mmap_read_lock_killable(mm)) @@ -7218,6 +7221,27 @@ static int __copy_remote_vm_str(struct mm_struct *mm, unsigned long addr, return buf - old_buf; } +/** + * copy_remote_mm_str - copy a string from a remote address space. + * @mm: the remote address space + * @addr: start address to read from + * @buf: destination buffer + * @len: number of bytes to copy + * @gup_flags: flags modifying lookup behaviour + * + * The caller must hold a reference on @mm. + * + * Return: number of bytes copied from @addr (source) to @buf (destination), + * not including the trailing NUL. If @len is zero, return 0 without accessing + * @buf. Otherwise, @buf is always NUL-terminated. On any error, return + * -EFAULT. + */ +int copy_remote_mm_str(struct mm_struct *mm, unsigned long addr, + void *buf, int len, unsigned int gup_flags) +{ + return __copy_remote_mm_str(mm, addr, buf, len, gup_flags); +} + /** * copy_remote_vm_str - copy a string from another process's address space. * @tsk: the task of the target address space @@ -7229,8 +7253,9 @@ static int __copy_remote_vm_str(struct mm_struct *mm, unsigned long addr, * The caller must hold a reference on @mm. * * Return: number of bytes copied from @addr (source) to @buf (destination); - * not including the trailing NUL. Always guaranteed to leave NUL-terminated - * buffer. On any error, return -EFAULT. + * not including the trailing NUL. If @len is zero, return 0 without accessing + * @buf. Otherwise, @buf is always NUL-terminated. On any error, return + * -EFAULT. */ int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr, void *buf, int len, unsigned int gup_flags) @@ -7247,7 +7272,7 @@ int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr, return -EFAULT; } - ret = __copy_remote_vm_str(mm, addr, buf, len, gup_flags); + ret = copy_remote_mm_str(mm, addr, buf, len, gup_flags); mmput(mm); diff --git a/mm/nommu.c b/mm/nommu.c index ed3934bc2de4..94e3709e95fd 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -1716,13 +1716,16 @@ EXPORT_SYMBOL_GPL(access_process_vm); * Copy a string from another process's address space as given in mm. * If there is any error return -EFAULT. */ -static int __copy_remote_vm_str(struct mm_struct *mm, unsigned long addr, +static int __copy_remote_mm_str(struct mm_struct *mm, unsigned long addr, void *buf, int len) { unsigned long addr_end; struct vm_area_struct *vma; int ret = -EFAULT; + if (unlikely(len == 0)) + return 0; + *(char *)buf = '\0'; if (mmap_read_lock_killable(mm)) @@ -1752,6 +1755,27 @@ static int __copy_remote_vm_str(struct mm_struct *mm, unsigned long addr, return ret; } +/** + * copy_remote_mm_str - copy a string from a remote address space. + * @mm: the remote address space + * @addr: start address to read from + * @buf: destination buffer + * @len: number of bytes to copy + * @gup_flags: flags modifying lookup behaviour (unused) + * + * The caller must hold a reference on @mm. + * + * Return: number of bytes copied from @addr (source) to @buf (destination), + * not including the trailing NUL. If @len is zero, return 0 without accessing + * @buf. Otherwise, @buf is always NUL-terminated. On any error, return + * -EFAULT. + */ +int copy_remote_mm_str(struct mm_struct *mm, unsigned long addr, + void *buf, int len, unsigned int gup_flags) +{ + return __copy_remote_mm_str(mm, addr, buf, len); +} + /** * copy_remote_vm_str - copy a string from another process's address space. * @tsk: the task of the target address space @@ -1763,8 +1787,9 @@ static int __copy_remote_vm_str(struct mm_struct *mm, unsigned long addr, * The caller must hold a reference on @mm. * * Return: number of bytes copied from @addr (source) to @buf (destination); - * not including the trailing NUL. Always guaranteed to leave NUL-terminated - * buffer. On any error, return -EFAULT. + * not including the trailing NUL. If @len is zero, return 0 without accessing + * @buf. Otherwise, @buf is always NUL-terminated. On any error, return + * -EFAULT. */ int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr, void *buf, int len, unsigned int gup_flags) @@ -1781,7 +1806,7 @@ int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr, return -EFAULT; } - ret = __copy_remote_vm_str(mm, addr, buf, len); + ret = copy_remote_mm_str(mm, addr, buf, len, gup_flags); mmput(mm); -- 2.55.0