From patchwork Tue Jan 28 22:43:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rome X-Patchwork-Id: 13953326 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 BDB5FC0218A for ; Tue, 28 Jan 2025 22:44:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EBFA828000E; Tue, 28 Jan 2025 17:44:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E6EEC28000D; Tue, 28 Jan 2025 17:44:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D0EF928000E; Tue, 28 Jan 2025 17:44:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id B1B6E28000D for ; Tue, 28 Jan 2025 17:44:22 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id DC8B3A0606 for ; Tue, 28 Jan 2025 22:44:21 +0000 (UTC) X-FDA: 83058340722.25.C9672DD Received: from mout.perfora.net (mout.perfora.net [74.208.4.196]) by imf14.hostedemail.com (Postfix) with ESMTP id 051F9100015 for ; Tue, 28 Jan 2025 22:44:19 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b=mveKIqSA; spf=pass (imf14.hostedemail.com: domain of linux@jordanrome.com designates 74.208.4.196 as permitted sender) smtp.mailfrom=linux@jordanrome.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738104260; 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:references:dkim-signature; bh=QRGkvncHhT4bF8p4emcNzTedGy4hijyVy88NPlXMXDw=; b=T1DX7B6B1uYe+72ejnUH9X5TTGxKWABHpiLrck/2fF1p65FuupAL+mVKLc/Nc17T8zeGWb urohma69NfwlyWIkXSFBi4kQ7IjZGTwfLi2/KRCqCCq5H/+DxREaa+keU3l1e8B3dFeE4Y +0e1XbxMJNqSOfR5scKmFPevDPxE0m8= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b=mveKIqSA; spf=pass (imf14.hostedemail.com: domain of linux@jordanrome.com designates 74.208.4.196 as permitted sender) smtp.mailfrom=linux@jordanrome.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738104260; a=rsa-sha256; cv=none; b=D+JSOEUzsabWnkdJiL3ieuoGNsGETT1VM33mK0T+kJ/ASVJi5tpELVP5UDUidnpLvskQI2 oG5rja0eY9Cm5GBPt7m5/iqtD/z0pfaRk8HgAQIEuMpOpNcK5kwAThBNjE5QABwcMye55Q JzamvwZ3LV4I+KztgW7DW9gaWmIYSxI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jordanrome.com; s=s1-ionos; t=1738104249; x=1738709049; i=linux@jordanrome.com; bh=QRGkvncHhT4bF8p4emcNzTedGy4hijyVy88NPlXMXDw=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:Message-ID: MIME-Version:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=mveKIqSAeXGSCvj82ClEFiCXgtX/+9Ofg8MPt5WQYjrE90Emhh1llJY7Qja+YzMi GldWW/Kwt7v47bUgKXYgDL+TMdFJdfWHwlGKqJd3clf9zKVDVKBpNgEQ1/ICUiOVE MY6Q6kzdgAFjS7YiYoBNSY/G3NQkBgvT/FzJtDoYoMNx1F1Np1Bhq3q7oUGz8JGm3 N+UDHheumeE8Nzkr7SJzJUA5cOoFfMzmFCK7xbKhZmnAZy94c8ZR1nM3x3nRLumea 7D6l1tc2JlG3nTVugNBaP7+syuYkrt8edAbxa29FLz9DucWxzRubPrL7UtOIbXLDM S3mQ9YXzF4W7N1V4HQ== X-UI-Sender-Class: 55c96926-9e95-11ee-ae09-1f7a4046a0f6 Received: from localhost ([69.171.251.9]) by mrelay.perfora.net (mreueus002 [74.208.5.2]) with ESMTPSA (Nemesis) id 0M2349-1tJRcR2j8t-014KM1; Tue, 28 Jan 2025 23:44:08 +0100 From: Jordan Rome To: bpf@vger.kernel.org Cc: linux-mm@kvack.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Kernel Team , Andrew Morton , Shakeel Butt , Alexander Potapenko Subject: [bpf-next v6 1/3] mm: add copy_remote_vm_str Date: Tue, 28 Jan 2025 14:43:50 -0800 Message-ID: <20250128224352.3808460-1-linux@jordanrome.com> X-Mailer: git-send-email 2.43.5 MIME-Version: 1.0 X-Provags-ID: V03:K1:ubzDyLLWS+xG74qCEuoWDefp+b9Z5apMh2VD+lUkxRhjhueh/zZ iPJyTViXfuRr3rNVDDyu/kllF8hZ3jXLWQiFYVJ2MVqtciXl0/T+07EiNt8EryJgJj+C9V8 +DIvoQb1C/5Ma0kW7R4lhpNWvJxJZOZv2/cYMTGWUxO17gDfjdvYVtStEEmWw/kHV0A6MIE wrplXUr8SvxVodW+/DT5w== UI-OutboundReport: notjunk:1;M01:P0:l5Al3IdDLUg=;+WvLmoKlwkVs0B6hB0P5cYDkcIg O4AzhMBo0WZWeFVL+hmkBYMIyMBjyxyltQ3pQ4ZQ7qnMEJ9LG0mowLNWJTva2WDKmlrX6ISFi BYUv+ZZQTSLu26SspQsqkS3G0kFmGbDRY0eGTxayZsUQ2gF7E0JR+VjradxsLbObeHRZKKYnv UY5IMC24vpj/RAGx5n+vW4vbnJ2iskIo7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Hvk6XpLJ6wna9jz0pEhiMxoDVphzE27YsLvc/7ENprog tc917Qas FBkHwWYobl74TIzExZIFK5VfaIwuZNKS2jPlMmdlFuk359hB2v7HciIRbMptXKkFZOHRVbmAvysP+8vaDJKqEayXX8lWzqb+t4VeldHwwYiEn6SBtVFVEUcFvhFN+YOpjSm3HsKoRVYseuc1/+1hVX9Xos3UA/MzcRGDYjKiLfp3e72U5cMVNBSXo7UOA1ChXgu6jNppafQa4+Cj4gJEAFSJwnaoIY7rzzWyJunT2jFLMRRAL5uMScyrvd3AhpYBonEY9yMOjhp0n4yP7TPnZwUqiTOs4NlrNDVov9EnNZJNnTv5pd2QPt1hWRqqEfSmLlESl6AK22h7Y/qa9lW+A1NYfQeGEImNSDcPhAQHVy5HzH5VMre55Nibwt1NDetUDBPf3 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: Similar to `access_process_vm` but specific to strings. Also chunks reads by page and utilizes `strscpy` for handling null termination. Signed-off-by: Jordan Rome --- include/linux/mm.h | 3 ++ mm/memory.c | 119 +++++++++++++++++++++++++++++++++++++++++++++ mm/nommu.c | 74 ++++++++++++++++++++++++++++ 3 files changed, 196 insertions(+) -- 2.43.5 diff --git a/include/linux/mm.h b/include/linux/mm.h index f02925447e59..f3a05b3eb2f2 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2485,6 +2485,9 @@ extern int access_process_vm(struct task_struct *tsk, unsigned long addr, extern int access_remote_vm(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); + long get_user_pages_remote(struct mm_struct *mm, unsigned long start, unsigned long nr_pages, unsigned int gup_flags, struct page **pages, diff --git a/mm/memory.c b/mm/memory.c index 398c031be9ba..7f6e74a99984 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -6714,6 +6714,125 @@ int access_process_vm(struct task_struct *tsk, unsigned long addr, } 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, + void *buf, int len, unsigned int gup_flags) +{ + void *old_buf = buf; + int err = 0; + ((char *)buf)[0] = '\0'; + + if (mmap_read_lock_killable(mm)) + return -EFAULT; + + /* Untag the address before looking up the VMA */ + addr = untagged_addr_remote(mm, addr); + + /* Avoid triggering the temporary warning in __get_user_pages */ + if (!vma_lookup(mm, addr)) { + err = -EFAULT; + goto out; + } + + while (len) { + int bytes, offset, retval; + void *maddr; + struct page *page; + struct vm_area_struct *vma = NULL; + + page = get_user_page_vma_remote(mm, addr, gup_flags, &vma); + + if (IS_ERR(page)) { + /* + * Treat as a total failure for now until we decide how + * to handle the CONFIG_HAVE_IOREMAP_PROT case and + * stack expansion. + */ + ((char *)buf)[0] = '\0'; + err = -EFAULT; + goto out; + } + + bytes = len; + offset = addr & (PAGE_SIZE - 1); + if (bytes > PAGE_SIZE - offset) + bytes = PAGE_SIZE - offset; + + maddr = kmap_local_page(page); + retval = strscpy(buf, maddr + offset, bytes); + + if (retval < 0) { + buf += (bytes - 1); + /* + * Because strscpy always NUL terminates we need to + * copy the last byte in the page if we are going to + * load more pages + */ + if (bytes != len) { + addr += (bytes - 1); + copy_from_user_page(vma, page, addr, buf, + maddr + (PAGE_SIZE - 1), 1); + + buf += 1; + addr += 1; + } + len -= bytes; + } + + unmap_and_put_page(page, maddr); + + if (retval >= 0) { + /* Found the end of the string */ + buf += retval; + goto out; + } + } + +out: + mmap_read_unlock(mm); + if (err) + return err; + + return buf - old_buf; +} + +/** + * copy_remote_vm_str - copy a string from another process's address space. + * @tsk: the task of the target 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. Always guaranteed to leave NUL-terminated + * buffer. 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) +{ + struct mm_struct *mm; + int ret; + + mm = get_task_mm(tsk); + if (!mm) { + ((char *)buf)[0] = '\0'; + return -EFAULT; + } + + ret = __copy_remote_vm_str(mm, addr, buf, len, gup_flags); + + mmput(mm); + + return ret; +} +EXPORT_SYMBOL_GPL(copy_remote_vm_str); + /* * Print the name of a VMA. */ diff --git a/mm/nommu.c b/mm/nommu.c index 9cb6e99215e2..4d83d0813eb8 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -1701,6 +1701,80 @@ int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, in } 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, + void *buf, int len) +{ + uint64_t tmp; + struct vm_area_struct *vma; + + int ret = -EFAULT; + + ((char *)buf)[0] = '\0'; + + if (mmap_read_lock_killable(mm)) + return ret; + + /* the access must start within one of the target process's mappings */ + vma = find_vma(mm, addr); + if (!vma) + goto out; + + if (check_add_overflow(addr, len, &tmp)) + goto out; + /* don't overrun this mapping */ + if (tmp >= vma->vm_end) + len = vma->vm_end - addr; + + /* only read mappings where it is permitted */ + if (vma->vm_flags & VM_MAYREAD) { + ret = strscpy(buf, (char *)addr, len); + if (ret < 0) + ret = len - 1; + } + +out: + mmap_read_unlock(mm); + return ret; +} + +/** + * copy_remote_vm_str - copy a string from another process's address space. + * @tsk: the task of the target 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. Always guaranteed to leave NUL-terminated + * buffer. 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) +{ + struct mm_struct *mm; + int ret; + + mm = get_task_mm(tsk); + if (!mm) { + ((char *)buf)[0] = '\0'; + return -EFAULT; + } + + ret = __copy_remote_vm_str(mm, addr, buf, len); + + mmput(mm); + + return ret; +} +EXPORT_SYMBOL_GPL(copy_remote_vm_str); + /** * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode * @inode: The inode to check