From patchwork Fri Mar 22 19:54:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Filippov X-Patchwork-Id: 13600416 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 D7871C47DD9 for ; Fri, 22 Mar 2024 19:56:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3630A6B0087; Fri, 22 Mar 2024 15:56:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2EC356B0088; Fri, 22 Mar 2024 15:56:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 18DE36B008A; Fri, 22 Mar 2024 15:56:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 042EE6B0087 for ; Fri, 22 Mar 2024 15:56:15 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id BF41AC054B for ; Fri, 22 Mar 2024 19:56:14 +0000 (UTC) X-FDA: 81925731468.26.307E6B9 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf04.hostedemail.com (Postfix) with ESMTP id 2F07A4001D for ; Fri, 22 Mar 2024 19:56:12 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Y9u/hV1G"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf04.hostedemail.com: domain of jcmvbkbc@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=jcmvbkbc@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711137373; 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=lkyJkArLDERo9g91DtAYBhws6AL+vDlqpRplXqxob5c=; b=USef9KRBs0gNhUbSCTm6faXOSUHqFmesxykoFQUT68t8vjwrY+GG9TFNaTGsEqsip8xqE8 vv4XESJDJaTm8wQ2KNFxCbyKiYECUuJ4Hu4/HsNgX2Zt9vN2fphY2YeVRzGiIFOGs/TkhZ JgWyEypFXWlQPUljkZs0MY+Zw0evQ9g= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Y9u/hV1G"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf04.hostedemail.com: domain of jcmvbkbc@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=jcmvbkbc@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711137373; a=rsa-sha256; cv=none; b=bGS9mMp4HmMmWPVzGvqCxmngceS/hICf/737NfAuCrOKg6ll8j5hN8/wY+IuDThNOvYqWw le8N34X9X/qIYB27/1tfqAdWeOM9uJoyx+H05eTdVcsOm8E+J6MdKfJMiPDVvD9gqyjx0D DWJ7tOr+Ip+vD8sVwV7qaPpGTVODLjw= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1e036b76725so18327255ad.1 for ; Fri, 22 Mar 2024 12:56:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711137372; x=1711742172; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=lkyJkArLDERo9g91DtAYBhws6AL+vDlqpRplXqxob5c=; b=Y9u/hV1Gf+myMLr3BdZeBavLhyXePjRfSJKHKjncvYJUoXLqFvxa2qnvtziPxhE0Da CfUTfnO4UePECrCgDq7H/HHfBnDNY+l3jKlMddhn+cKpaLNHejJNHgGGuAdc/ppJm38S pKTs5ypljsfRBQS7VrW02Bh9JL0uFKGR+1HGWheHNyRDmALpaCv+zQzOZCOVjU7kvXKN Kqqtj+9bsrZtTraHVmgtwTjMjLuQGDqY2NhgEp2pYZSKedwAy1oGsyxDI03bI6qEcL6j WEoMM5KS/E/T2tigzDCcLKzBjhVVLjToH2u2NQtJ9rtQAyI57BWXkRogt+0oMt8NvOpg FXdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711137372; x=1711742172; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=lkyJkArLDERo9g91DtAYBhws6AL+vDlqpRplXqxob5c=; b=TmP1+slWQWx5nTXtsiXwNkz1YUl02Mux5TQD7MFvIYNRYth9ORE05q+i0jPo1ouKDE K41Txqh+8vCIbeuPShavFLkqAm0th7ftCivOCgrts6mKC3uC+pCFEiw26Zy+CzVk3xt7 VFlVDJXuXx6p75YQIT0WUBbMPqTzeE+KInkUSY46NK1KclRYKPCCrsOChgkxzKUilN3P 3ccbYAVX6fffB/gJKqHJ6MOaQPeWeZpqu7ZH5sgGLadKJjU9O5odl6J86N/jsJwOjpyI sfFJtFQmIfjZ33yQfHF9leLedw0OtHDXFqzm/gIyoCwSL58A+c6JmyMCDZh/mBMDTV0f 4DUA== X-Gm-Message-State: AOJu0YyHMGPOtuX6EcLdn3rJYRK8XF9AWsO2Jm2+AgdumlVFwiJfzVKi E386ZgfO8A+aJ1WtRJQb2AQTIC1tQV6yxSvbsuUgoLkT72fnQl9w X-Google-Smtp-Source: AGHT+IF6ob1NPeuDZPbaHWZhsuj5S+pJqN8Jp54xrN2M5U36hty3pL3eFm8RTKBnSj4PZ0J/zhBa9A== X-Received: by 2002:a17:902:a513:b0:1e0:4aac:e547 with SMTP id s19-20020a170902a51300b001e04aace547mr616154plq.58.1711137371807; Fri, 22 Mar 2024 12:56:11 -0700 (PDT) Received: from octofox.hsd1.ca.comcast.net ([2601:646:a200:bbd0:710e:e701:408c:9832]) by smtp.gmail.com with ESMTPSA id jo6-20020a170903054600b001dcc2951c02sm112628plb.286.2024.03.22.12.56.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 12:56:11 -0700 (PDT) From: Max Filippov To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Eric Biederman , Kees Cook , Alexander Viro , Christian Brauner , Jan Kara , Max Filippov Subject: [PATCH] binfmt_elf_fdpic: fix /proc//auxv Date: Fri, 22 Mar 2024 12:54:18 -0700 Message-Id: <20240322195418.2160164-1-jcmvbkbc@gmail.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Rspamd-Queue-Id: 2F07A4001D X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: iay3pkiw5rpxn8pfxban7pm47urfkn4g X-HE-Tag: 1711137372-750420 X-HE-Meta: U2FsdGVkX1+2O76AtaeXLX91Z85c4JqC4e45lmKf9KRZJ2d98u/THIzgXSwkmU5pJVsFXyoYI7zGCk0TbY17+IEsQHmJZkBlH27kdpHf+yNbSQ5b6zHE1+C0IbgiucIzXG2j+HOKl2m65bftVcERQZ2d1STtR0cse3Oi7pXY3p6OktKnZUVHytPpohIygnGHXvrIxfpkn+lsMchEKW1TagMpJd+gA8pX0RZE6DM6e0wsPjGDj4da1tUZoszzaNeXbXOwoa0AqE6t8OUsMl1Z17mfMN6a1qHbt5g8hKJEwrpi8FHV2l4Or5YTx5XlUr/+7VpWAXOIxbB/zg/S319eTdlXcMwRK5SikXtn/tlxBZV+HG14AbWWuFu1XKB/r8gVctZ7+8gbeMM9Ng4OqAsuZulc+HqvHXb9/dShxXmgsCqHcBIfsINOYysTgJd3iYSsiu1c79uwV1dSvTO5J2NnfXEtcY0rvDc4NzA2orj2iSl1V+DeX7mvHlA0usYATDMC98iIpA9r8p/LsE6Bmi7Dtk1YmmP3yZW5JWbfO4D+QznF2OZH0vN3G9muTe41kURxFMIt5MCht/WZPNrtBFrWW5oY+mb8juJnYt5I2GqkSv0CmFWvnkOJjQMlorKWwnrwXtTBzraeYouHML5Fx8bLJbbks7Ry/ZKxZCxF06VvVKPxoMaw+VhXUOmtln2UAoCTDixdi64GQU2PIhJ1knXl9+AFf8FTDyvN8RFIZ3zhmQJvbgfEcLfV1lBZCdoQM8ZrmfdmFsRVhAG709JCNcxwB9JufSL4Bx+sUbcqpyj6G6XyClmLazVnwtb45Uz5ULtmniW7LEzMOzEUPgVOk4xq9aZmXoFkqze5lCmMrEwMjDN/gOr7sgFCdhfj3Xebg/8Fsl5h52y0+gtis0GH7ARV28H+Ujt0jBjJfvOE62LbJ09N3JjBn5cIo+KCQVU07Tc9E7D2CJeKSlo8lofhTpb NAfsi3HF FK/rC+PwJyH3l3ZpqXzYHcVDjxYzvQRwwILDYrWBjMwJ8H4gmbU0k/uf4Hw4nXEpYsYgDoHPNg48Puk6dcrgDVmsra+SRQ55EGbPKYWyhSUSxmmXC6I5A4OqH1dPdfP/AKt/lMog5y6lMgVlqtrUry+376smyO4vkL9aD 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: Althought FDPIC linux kernel provides /proc//auxv files they are empty because there's no code that initializes mm->saved_auxv in the FDPIC ELF loader. Synchronize FDPIC ELF aux vector setup with ELF. Replace entry-by-entry aux vector copying to userspace with initialization of mm->saved_auxv first and then copying it to userspace as a whole. Signed-off-by: Max Filippov --- fs/binfmt_elf_fdpic.c | 88 +++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 46 deletions(-) diff --git a/fs/binfmt_elf_fdpic.c b/fs/binfmt_elf_fdpic.c index fefc642541cb..7b4542a0cbe3 100644 --- a/fs/binfmt_elf_fdpic.c +++ b/fs/binfmt_elf_fdpic.c @@ -505,8 +505,9 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm, char *k_platform, *k_base_platform; char __user *u_platform, *u_base_platform, *p; int loop; - int nr; /* reset for each csp adjustment */ unsigned long flags = 0; + int ei_index; + elf_addr_t *elf_info; #ifdef CONFIG_MMU /* In some cases (e.g. Hyper-Threading), we want to avoid L1 evictions @@ -601,44 +602,24 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm, csp -= sp & 15UL; sp -= sp & 15UL; - /* put the ELF interpreter info on the stack */ -#define NEW_AUX_ENT(id, val) \ - do { \ - struct { unsigned long _id, _val; } __user *ent, v; \ - \ - ent = (void __user *) csp; \ - v._id = (id); \ - v._val = (val); \ - if (copy_to_user(ent + nr, &v, sizeof(v))) \ - return -EFAULT; \ - nr++; \ + /* Create the ELF interpreter info */ + elf_info = (elf_addr_t *)mm->saved_auxv; + /* update AT_VECTOR_SIZE_BASE if the number of NEW_AUX_ENT() changes */ +#define NEW_AUX_ENT(id, val) \ + do { \ + *elf_info++ = id; \ + *elf_info++ = val; \ } while (0) - nr = 0; - csp -= 2 * sizeof(unsigned long); - NEW_AUX_ENT(AT_NULL, 0); - if (k_platform) { - nr = 0; - csp -= 2 * sizeof(unsigned long); - NEW_AUX_ENT(AT_PLATFORM, - (elf_addr_t) (unsigned long) u_platform); - } - - if (k_base_platform) { - nr = 0; - csp -= 2 * sizeof(unsigned long); - NEW_AUX_ENT(AT_BASE_PLATFORM, - (elf_addr_t) (unsigned long) u_base_platform); - } - - if (bprm->have_execfd) { - nr = 0; - csp -= 2 * sizeof(unsigned long); - NEW_AUX_ENT(AT_EXECFD, bprm->execfd); - } - - nr = 0; - csp -= DLINFO_ITEMS * 2 * sizeof(unsigned long); +#ifdef ARCH_DLINFO + /* + * ARCH_DLINFO must come first so PPC can do its special alignment of + * AUXV. + * update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT() in + * ARCH_DLINFO changes + */ + ARCH_DLINFO; +#endif NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP); #ifdef ELF_HWCAP2 NEW_AUX_ENT(AT_HWCAP2, ELF_HWCAP2); @@ -659,17 +640,32 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm, NEW_AUX_ENT(AT_EGID, (elf_addr_t) from_kgid_munged(cred->user_ns, cred->egid)); NEW_AUX_ENT(AT_SECURE, bprm->secureexec); NEW_AUX_ENT(AT_EXECFN, bprm->exec); + if (k_platform) { + NEW_AUX_ENT(AT_PLATFORM, + (elf_addr_t)(unsigned long)u_platform); + } + if (k_base_platform) { + NEW_AUX_ENT(AT_BASE_PLATFORM, + (elf_addr_t)(unsigned long)u_base_platform); + } + if (bprm->have_execfd) { + NEW_AUX_ENT(AT_EXECFD, bprm->execfd); + } +#undef NEW_AUX_ENT + /* AT_NULL is zero; clear the rest too */ + memset(elf_info, 0, (char *)mm->saved_auxv + + sizeof(mm->saved_auxv) - (char *)elf_info); -#ifdef ARCH_DLINFO - nr = 0; - csp -= AT_VECTOR_SIZE_ARCH * 2 * sizeof(unsigned long); + /* And advance past the AT_NULL entry. */ + elf_info += 2; - /* ARCH_DLINFO must come last so platform specific code can enforce - * special alignment requirements on the AUXV if necessary (eg. PPC). - */ - ARCH_DLINFO; -#endif -#undef NEW_AUX_ENT + ei_index = elf_info - (elf_addr_t *)mm->saved_auxv; + csp -= ei_index * sizeof(elf_addr_t); + + /* Put the elf_info on the stack in the right place. */ + if (copy_to_user((void __user *)csp, mm->saved_auxv, + ei_index * sizeof(elf_addr_t))) + return -EFAULT; /* allocate room for argv[] and envv[] */ csp -= (bprm->envc + 1) * sizeof(elf_caddr_t);