Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp947704pxb; Wed, 6 Apr 2022 05:05:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyA40A2UPymqFG9bLZ4YL6Ia2R0nnxXvHE1PFx5if2vJ8UqQEhab7Ytluz8VwFx4wMFw0gw X-Received: by 2002:a92:ae02:0:b0:2c6:798d:2be with SMTP id s2-20020a92ae02000000b002c6798d02bemr3704661ilh.18.1649246701151; Wed, 06 Apr 2022 05:05:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649246701; cv=none; d=google.com; s=arc-20160816; b=C76w2UUYXOxQfXZ2tZxDgaT5CbQGYYUFVIqDM2BN+7B+eGir5uubx8O1l8P8nSh7db BuxkW0EW3FKWnCk8UuaC5RN/gpc/7ulG8a5XL6Gl4gm3Bbtf/W9hlw22lSvFah5/rl68 YgwaJmTSzvnTjgZU317fu58m1e+vykXPFriTP66z0NMvQHy9Yhq0gt6dk9mwSQ6Dt7Gp I1V5eRkPlUXVa+YgmVwlvTuXcJADOlNktDqj5LErErpbw55pmZS7XypH32eHMFCPZCcO afqS/5QNbzeluCPOe6bFkOihyILAxv1AAb2IyEzBk/7GKpRM+26bjogARujKuJlyiCrF w/RA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=I3+jOKfAbm3DtzBo9TNUsJeL4uQK4pinZUo9AvR3qVI=; b=KoSv6V85fLc+jP0JwmMqpVbnT7S1J0hOjQgI4EG+CoHRJ2Iym+rzDyuDogQFh/Jjxp Oita1NGxZyqXWmn2jsh+0QuI3ndlxzJIaBEAav6MhMZyusZXoUbHxHXWfoRENWUYKKYK kCkbM1zbbzA2vDUhuSWweR1j6iINDyjLzixZR8NcedeJLFyiNJeBjqwLR1JA/JXMly+z 7dF3AynaT23WBLik+0uWSoyafM6BebThlyYR/IwJKw/cKtspATRN/DNZb9XUv6jx/IGF iFuh+FwaHytGSmIA4hJqEydlQ2NWO1YxZthx3QWvzRF2Kg/aF573EW43GeESihMut2x+ NQkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=nf4cRbjJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id h2-20020a056602130200b0064c75752c8asi7991161iov.6.2022.04.06.05.05.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 05:05:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=nf4cRbjJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8598967C7C8; Wed, 6 Apr 2022 03:32:25 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233743AbiDEVUp (ORCPT + 99 others); Tue, 5 Apr 2022 17:20:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351905AbiDEKD3 (ORCPT ); Tue, 5 Apr 2022 06:03:29 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E114BA2069; Tue, 5 Apr 2022 02:52:37 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 175B361750; Tue, 5 Apr 2022 09:52:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 21732C385A2; Tue, 5 Apr 2022 09:52:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649152356; bh=v1V7OgQ9ZfL/uDAaRTe7sePn9Z+Wh9PgpBSkw0TQrrs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nf4cRbjJgBAlqtk/6QdigGLeYTDCVFoT5wMHsefby2U+KQFotqoenuexeZFpHEagj DbBeqJsSDSPDY2qE+DwEBlJsY7+GfDh7FUsbhqeIj6zNBQuu4ZZu4ME4HsBU/g3Bue hh/Ol2KyPvDEc5A5w99N6a1Hgy0WpAs+N2hdWiA4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Akira Kawata , kernel test robot , Kees Cook , Sasha Levin Subject: [PATCH 5.15 699/913] fs/binfmt_elf: Fix AT_PHDR for unusual ELF files Date: Tue, 5 Apr 2022 09:29:21 +0200 Message-Id: <20220405070400.787359881@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Akira Kawata [ Upstream commit 0da1d5002745cdc721bc018b582a8a9704d56c42 ] BugLink: https://bugzilla.kernel.org/show_bug.cgi?id=197921 As pointed out in the discussion of buglink, we cannot calculate AT_PHDR as the sum of load_addr and exec->e_phoff. : The AT_PHDR of ELF auxiliary vectors should point to the memory address : of program header. But binfmt_elf.c calculates this address as follows: : : NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff); : : which is wrong since e_phoff is the file offset of program header and : load_addr is the memory base address from PT_LOAD entry. : : The ld.so uses AT_PHDR as the memory address of program header. In normal : case, since the e_phoff is usually 64 and in the first PT_LOAD region, it : is the correct program header address. : : But if the address of program header isn't equal to the first PT_LOAD : address + e_phoff (e.g. Put the program header in other non-consecutive : PT_LOAD region), ld.so will try to read program header from wrong address : then crash or use incorrect program header. This is because exec->e_phoff is the offset of PHDRs in the file and the address of PHDRs in the memory may differ from it. This patch fixes the bug by calculating the address of program headers from PT_LOADs directly. Signed-off-by: Akira Kawata Reported-by: kernel test robot Acked-by: Kees Cook Signed-off-by: Kees Cook Link: https://lore.kernel.org/r/20220127124014.338760-2-akirakawata1@gmail.com Signed-off-by: Sasha Levin --- fs/binfmt_elf.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index a813b70f594e..3f6a7cac68fd 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -170,8 +170,8 @@ static int padzero(unsigned long elf_bss) static int create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec, - unsigned long load_addr, unsigned long interp_load_addr, - unsigned long e_entry) + unsigned long interp_load_addr, + unsigned long e_entry, unsigned long phdr_addr) { struct mm_struct *mm = current->mm; unsigned long p = bprm->p; @@ -257,7 +257,7 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec, NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP); NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE); NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC); - NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff); + NEW_AUX_ENT(AT_PHDR, phdr_addr); NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr)); NEW_AUX_ENT(AT_PHNUM, exec->e_phnum); NEW_AUX_ENT(AT_BASE, interp_load_addr); @@ -823,7 +823,7 @@ static int parse_elf_properties(struct file *f, const struct elf_phdr *phdr, static int load_elf_binary(struct linux_binprm *bprm) { struct file *interpreter = NULL; /* to shut gcc up */ - unsigned long load_addr = 0, load_bias = 0; + unsigned long load_addr, load_bias = 0, phdr_addr = 0; int load_addr_set = 0; unsigned long error; struct elf_phdr *elf_ppnt, *elf_phdata, *interp_elf_phdata = NULL; @@ -1156,6 +1156,17 @@ static int load_elf_binary(struct linux_binprm *bprm) reloc_func_desc = load_bias; } } + + /* + * Figure out which segment in the file contains the Program + * Header table, and map to the associated memory address. + */ + if (elf_ppnt->p_offset <= elf_ex->e_phoff && + elf_ex->e_phoff < elf_ppnt->p_offset + elf_ppnt->p_filesz) { + phdr_addr = elf_ex->e_phoff - elf_ppnt->p_offset + + elf_ppnt->p_vaddr; + } + k = elf_ppnt->p_vaddr; if ((elf_ppnt->p_flags & PF_X) && k < start_code) start_code = k; @@ -1191,6 +1202,7 @@ static int load_elf_binary(struct linux_binprm *bprm) } e_entry = elf_ex->e_entry + load_bias; + phdr_addr += load_bias; elf_bss += load_bias; elf_brk += load_bias; start_code += load_bias; @@ -1254,8 +1266,8 @@ static int load_elf_binary(struct linux_binprm *bprm) goto out; #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */ - retval = create_elf_tables(bprm, elf_ex, - load_addr, interp_load_addr, e_entry); + retval = create_elf_tables(bprm, elf_ex, interp_load_addr, + e_entry, phdr_addr); if (retval < 0) goto out; -- 2.34.1