Received: by 2002:ab2:69cc:0:b0:1fd:c486:4f03 with SMTP id n12csp263351lqp; Tue, 11 Jun 2024 04:02:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVc2vXCjSR84jyKpGJd18TsKXQO976NhkpjEOk9dgmttbTJrmNK5vXwa924iuDw6nuZRB//U27J9p7smASvzQL/dIF5Fwuw3G2qyAB+nw== X-Google-Smtp-Source: AGHT+IGiRLEpLMCTB8k0MgG6dKMKoJ4t4Ad1CStq2Boig/j7CXRDvTxZZvSB7D+CsAVJKA2kZPwp X-Received: by 2002:ac8:7dcf:0:b0:440:5bd6:15a with SMTP id d75a77b69052e-4405bd602c0mr103085501cf.14.1718103729284; Tue, 11 Jun 2024 04:02:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718103729; cv=pass; d=google.com; s=arc-20160816; b=vOYZzXvyaEXSgCFbkusAJhDTwf0ZKE9XTUQCsULBDSiZZHsm2OI3YiWbpwoEU2L33r +ULfijn7857R7STGPK154Xp58C3WuqmqAfnyeT7IyAuy1m+HQjB1xsAJdsNjlDW25XlG rJA5CsY3+nRCUta/7wpiI8a7KwNafkv6IgIYhtESJGZVgkBd/133ePaAaMoafh5LBx/N 9vk2NFlYeVUXk7JS1P9oGTliweTPx9YVrO4rmv5k4OpBmQc/at4msq4PvEkulKg6+2r/ AQqahz7f0pqkUOHD0Xx3JfbXbEntgH8MkFAOjJqTHh/mxu0EtXv5FCoo+tPzWwgzwxB1 LY6g== 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=zFIAdRJjcqVY4q+WP7p/dSviF8VvQtCH3OihxvuS13I=; fh=Y/y/ztjXR7aEronvE26r4mZ0eg4Xsop0N9w5JVKmVVc=; b=obD3M0XY1RhfMv48ziom8vIUijMvaIEG6b+8mxv4iiTZOQhn0Y0vzJnpOEjee0gNKJ DSqet1t/RbkBRCFim1YRNBQzSSgEgHAfztXWeN4qYccWDzTKxM9BDdIgzEmphEw6/MFm f4WgQzUZQMhn6b82Aki2GsCUVhUc2cKOQvZ3GHNznO05ACkOYWv2EUviAk8GwRNCS6dh tm3wVdiJ11Ay/fAm3sAN8UmZgoZLulK55SHlZt7drAk9zXeODzxVQwd1NE0z+2wrPXfP mr5ZJGZvnNWe3RSn6AwqPDLoNgKkU9TIwA6Bt7zJXy8ZBrjRZ7FUwBdYFpyZLFje1DF9 geTQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DaXC3o00; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-209691-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-209691-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d75a77b69052e-44038b8495bsi122851691cf.760.2024.06.11.04.02.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jun 2024 04:02:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-209691-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DaXC3o00; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-209691-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-209691-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.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 ny.mirrors.kernel.org (Postfix) with ESMTPS id EDED31C229F4 for ; Tue, 11 Jun 2024 11:02:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E10BB17BB10; Tue, 11 Jun 2024 11:01:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DaXC3o00" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA88217B511; Tue, 11 Jun 2024 11:01:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718103671; cv=none; b=INIlG1xirYGDIW/UxEqG2SmHUsMGiY9TWdu8NOMNnEmkEu5XBdIqj8LfVxfQtbrWCzZIDF/JbAJUp8WqWa8Xco35lRAZs/UmsrFmOkoaWITZRIkkdXh3R/g6vmGIFZrNxnWYfnAkEbCxkqNze43kZPQ2BvRLusFi9vsQ+zwLd7c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718103671; c=relaxed/simple; bh=ABTqOOEfJVePTVcWRG7oSFkHsd6dWZCvE/bT2jUog+g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nCj9AlfUr7nPpn75A6ZSZWvJE3Vg2cR1AGluM96TXAYkYq+XVGukkHFN3zuytLK93fdzAcQ869HVlGT3RoF2SfA5HCFFLc5mMu8ydTW97DYZE+dfT4IqcAHfhl/OLuZOjbAUPfOkdiTayF+J/8HKoX2hHEzkhnaVa7rq5EHWz0g= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DaXC3o00; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8CB6DC32786; Tue, 11 Jun 2024 11:01:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718103670; bh=ABTqOOEfJVePTVcWRG7oSFkHsd6dWZCvE/bT2jUog+g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DaXC3o00deV4bsRGzIaS6LGKPgx1tlb6kfI0ATeFD8qe8eaCxLMfxs6oWUJ3NEDoZ LnwDTE5zKfKzXnSieLjOO9UxeqxOGl990nXF89pnpwx6oT3/Y7j8Dkp0+iO8WAXdu8 D2/gjoshpRHXKiEve8pTgdBmIhSzEvjhzCL7GcjLsuQnpeWbwT5LMPAHo/z2QPjJKH /I1oD8k3R2mPjOuIh0g5yvdiUzS9+OH5r66mSmaK5qK18RZ87+IVn2sZKlj2Sayux+ 6EikV3EWJemXTWuP2i4M9jgZCrnfrq0wD2oyOwayby8F2pybWGNGQSv5UBlVyO8Y6t BNAnsuQxODGLw== From: Andrii Nakryiko To: linux-fsdevel@vger.kernel.org, brauner@kernel.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, gregkh@linuxfoundation.org, linux-mm@kvack.org, liam.howlett@oracle.com, surenb@google.com, rppt@kernel.org, Andrii Nakryiko Subject: [PATCH v4 3/7] fs/procfs: add build ID fetching to PROCMAP_QUERY API Date: Tue, 11 Jun 2024 04:00:51 -0700 Message-ID: <20240611110058.3444968-4-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240611110058.3444968-1-andrii@kernel.org> References: <20240611110058.3444968-1-andrii@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The need to get ELF build ID reliably is an important aspect when dealing with profiling and stack trace symbolization, and /proc//maps textual representation doesn't help with this. To get backing file's ELF build ID, application has to first resolve VMA, then use it's start/end address range to follow a special /proc//map_files/- symlink to open the ELF file (this is necessary because backing file might have been removed from the disk or was already replaced with another binary in the same file path. Such approach, beyond just adding complexity of having to do a bunch of extra work, has extra security implications. Because application opens underlying ELF file and needs read access to its entire contents (as far as kernel is concerned), kernel puts additional capable() checks on following /proc//map_files/- symlink. And that makes sense in general. But in the case of build ID, profiler/symbolizer doesn't need the contents of ELF file, per se. It's only build ID that is of interest, and ELF build ID itself doesn't provide any sensitive information. So this patch adds a way to request backing file's ELF build ID along the rest of VMA information in the same API. User has control over whether this piece of information is requested or not by either setting build_id_size field to zero or non-zero maximum buffer size they provided through build_id_addr field (which encodes user pointer as __u64 field). This is a completely optional piece of information, and so has no performance implications for user cases that don't care about build ID, while improving performance and simplifying the setup for those application that do need it. Kernel already implements build ID fetching, which is used from BPF subsystem. We are reusing this code here, but plan a follow up changes to make it work better under more relaxed assumption (compared to what existing code assumes) of being called from user process context, in which page faults are allowed. BPF-specific implementation currently bails out if necessary part of ELF file is not paged in, all due to extra BPF-specific restrictions (like the need to fetch build ID in restrictive contexts such as NMI handler). Signed-off-by: Andrii Nakryiko --- fs/proc/task_mmu.c | 25 ++++++++++++++++++++++++- include/uapi/linux/fs.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 534f2dbb5175..80543aed7ddd 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -445,6 +446,7 @@ static struct vm_area_struct *query_matching_vma(struct mm_struct *mm, addr = vma->vm_end; if (flags & PROCMAP_QUERY_COVERING_OR_NEXT_VMA) goto next_vma; + no_vma: return ERR_PTR(-ENOENT); } @@ -455,7 +457,7 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg) struct vm_area_struct *vma; struct mm_struct *mm; const char *name = NULL; - char *name_buf = NULL; + char build_id_buf[BUILD_ID_SIZE_MAX], *name_buf = NULL; __u64 usize; int err; @@ -477,6 +479,8 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg) /* either both buffer address and size are set, or both should be zero */ if (!!karg.vma_name_size != !!karg.vma_name_addr) return -EINVAL; + if (!!karg.build_id_size != !!karg.build_id_addr) + return -EINVAL; mm = priv->mm; if (!mm || !mmget_not_zero(mm)) @@ -522,6 +526,21 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg) if (vma->vm_flags & VM_MAYSHARE) karg.vma_flags |= PROCMAP_QUERY_VMA_SHARED; + if (karg.build_id_size) { + __u32 build_id_sz; + + err = build_id_parse(vma, build_id_buf, &build_id_sz); + if (err) { + karg.build_id_size = 0; + } else { + if (karg.build_id_size < build_id_sz) { + err = -ENAMETOOLONG; + goto out; + } + karg.build_id_size = build_id_sz; + } + } + if (karg.vma_name_size) { size_t name_buf_sz = min_t(size_t, PATH_MAX, karg.vma_name_size); const struct path *path; @@ -566,6 +585,10 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg) } kfree(name_buf); + if (karg.build_id_size && copy_to_user((void __user *)karg.build_id_addr, + build_id_buf, karg.build_id_size)) + return -EFAULT; + if (copy_to_user(uarg, &karg, min_t(size_t, sizeof(karg), usize))) return -EFAULT; diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h index f25e7004972d..7306022780d3 100644 --- a/include/uapi/linux/fs.h +++ b/include/uapi/linux/fs.h @@ -509,6 +509,26 @@ struct procmap_query { * If set to zero, vma_name_addr should be set to zero as well */ __u32 vma_name_size; /* in/out */ + /* + * If set to non-zero value, signals the request to extract and return + * VMA's backing file's build ID, if the backing file is an ELF file + * and it contains embedded build ID. + * + * Kernel will set this field to zero, if VMA has no backing file, + * backing file is not an ELF file, or ELF file has no build ID + * embedded. + * + * Build ID is a binary value (not a string). Kernel will set + * build_id_size field to exact number of bytes used for build ID. + * If build ID is requested and present, but needs more bytes than + * user-supplied maximum buffer size (see build_id_addr field below), + * -E2BIG error will be returned. + * + * If this field is set to non-zero value, build_id_addr should point + * to valid user space memory buffer of at least build_id_size bytes. + * If set to zero, build_id_addr should be set to zero as well + */ + __u32 build_id_size; /* in/out */ /* * User-supplied address of a buffer of at least vma_name_size bytes * for kernel to fill with matched VMA's name (see vma_name_size field @@ -517,6 +537,14 @@ struct procmap_query { * Should be set to zero if VMA name should not be returned. */ __u64 vma_name_addr; /* in */ + /* + * User-supplied address of a buffer of at least build_id_size bytes + * for kernel to fill with matched VMA's ELF build ID, if available + * (see build_id_size field description above for details). + * + * Should be set to zero if build ID should not be returned. + */ + __u64 build_id_addr; /* in */ }; #endif /* _UAPI_LINUX_FS_H */ -- 2.43.0