Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp1570452pxb; Thu, 7 Oct 2021 10:22:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwMn4D+qb9vAC6FD1/U37oQr57j8qUsKQ4wDn2omCTLobzvF7o7InzDI/MEye2ytEcNGrHs X-Received: by 2002:a62:8cc4:0:b0:44c:a611:bf53 with SMTP id m187-20020a628cc4000000b0044ca611bf53mr5235053pfd.64.1633627325488; Thu, 07 Oct 2021 10:22:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633627325; cv=none; d=google.com; s=arc-20160816; b=Wa5pbSrkUnEz0z55Cbou6xVeRBYE1QKmqUmmk1wtyUhtjs+WlkkTJdwpDpEpyl0qLq Q03DXQ9CqCP+GcRzm3ffmc/QAFonfagyzkzbMjqr/bZGthaeBGU63y3YSk6CuiX/xNzp 9c8yjYhONk74VV4fprilj44RFf40bEHygKeAyZoaNvfPgJ3oz/ukRkZzD4UBrbeecoeG PxdqPpmZdgZQc9BAwIYU2U+wi7kxu1VZ+R2WEmuswm8Pi6hmd+g2+NBAcRsC+TWgyYvR WLeBozFdQ5zOVBK0bl2oa0Gj2dx1rNhPulNUpHXCd5MxJDqeMHC/8OcxNaD2/ameGVN0 bwnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=yKsD+bnBh7ZowB88TXB3Da8yHTszLtLZ5FscyLH62G8=; b=MaNd+INi5mkPQS5DERcTzCrn5uHyjIhCVYH6D2ouGmq0Sl2+jYIBCG3M5Ug4KYUzGT iVTEIi2IK0R4gYPgTKBjeiRGClpW9F3oaHwVET2DmFT9ytbDZ/VLOlJi4WUrvgwTYHty ZVTzliGw/G3JHbBApO1fAuLr66VSj7ibVgceDI7pUOPyiJB1raw/I/6U/w37iTRGOGQ2 lx467nCTu6UrwjXxsWlsGPHKYvrh5F7/Yu8sL+Bowb7GyNns/ujmR2MU48cvigNkrUOU fUcFDMrls6uGn0olrEcUH3dboixZ6Yz+y7xhIR3jPEuW4ioxeZ9AFT+DFs8rI61CFF7j VsMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=qzJrudNA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z7si208301plo.135.2021.10.07.10.21.52; Thu, 07 Oct 2021 10:22:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=qzJrudNA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242576AbhJGRWL (ORCPT + 99 others); Thu, 7 Oct 2021 13:22:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243314AbhJGRWB (ORCPT ); Thu, 7 Oct 2021 13:22:01 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA1BBC061755 for ; Thu, 7 Oct 2021 10:20:06 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id t16so4007136eds.9 for ; Thu, 07 Oct 2021 10:20:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=yKsD+bnBh7ZowB88TXB3Da8yHTszLtLZ5FscyLH62G8=; b=qzJrudNAbjJ/u+lxCUM3pJBXvEy8hefF83Q86cfsnLIin8POqkGg0iuOyBIR4DlTp0 K2oM59cTFvIVXUM3XbgezLXqKRoa8rVCPeK94yyzT8E6iDfmrIPKTapu3/Qzf4FNUrEm /F9kl8OSoIsJvd++1BVBabNT2J9ORCI9wjGML666IbcpYE4nV5CMOHHOgqxkfMSLTiVU gSH2G2QJ8eR4bCJv9eF5NQni6/OBtEESs4WV9YWEThjGTrKbAyZejJ7Ve+d3sXznupud iNFEiG/gVwSrDr6qmfpqTFuWscx8Pls0/026nK0PEc/WfbRZU9T3UXrFVZTV61pDMnIi lAKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=yKsD+bnBh7ZowB88TXB3Da8yHTszLtLZ5FscyLH62G8=; b=ASPoTpC3kAl7FlyGTwhD1MQ+gIjQaMPBeaJkd6IpPA7L8t2qG0EIz0QnHxIinRPR8a x4BjOcQC9xl4+Z2RW/8gjqKc0ysSdPMlIzY7+Z0x/YINuj9u3uSNffE4/XidCWF2PExo JAAjRW+gwlRB0DNJyVnU7HBUi6ACzYXffQUvxfs2dPvbASrSQ6MPvOT7LlBBIQWvSCU4 c7kF6htYyabUCMd51VwIz/U/IPXl6aZnBtU4eBRMiR6WvMOnuRsvgMKACuDUpj18Xmuj YxykQPR8jDnmRDvxbrToYsWGALKC4JT1WvcXYAUCvjqHOjHkMOtOS2eqydWpuzrKcU7n JySw== X-Gm-Message-State: AOAM531Sm5cu1UdRbimEZCJzazhxc78BQj1R9YyBl9FcXa1Qhmf7AW5I DO+DZ/tdsCW40IEWRZADtuDiKXEPfA== X-Received: by 2002:a50:eb9a:: with SMTP id y26mr7684875edr.186.1633627205494; Thu, 07 Oct 2021 10:20:05 -0700 (PDT) Received: from localhost.localdomain ([46.53.254.50]) by smtp.gmail.com with ESMTPSA id n10sm4937ejk.86.2021.10.07.10.20.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Oct 2021 10:20:05 -0700 (PDT) Date: Thu, 7 Oct 2021 20:20:03 +0300 From: Alexey Dobriyan To: Kees Cook Cc: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] ELF: fix overflow in total mapping size calculation Message-ID: References: <202110051929.37279B6B4A@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <202110051929.37279B6B4A@keescook> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 05, 2021 at 07:31:09PM -0700, Kees Cook wrote: > On Sun, Oct 03, 2021 at 03:11:24PM +0300, Alexey Dobriyan wrote: > > Kernel assumes that ELF program headers are ordered by mapping address, > > but doesn't enforce it. It is possible to make mapping size extremely huge > > by simply shuffling first and last PT_LOAD segments. > > > > As long as PT_LOAD segments do not overlap, it is silly to require > > sorting by v_addr anyway because mmap() doesn't care. > > > > Don't assume PT_LOAD segments are sorted and calculate min and max > > addresses correctly. > > Nice! Yes, this all make sense. > > > > > Signed-off-by: Alexey Dobriyan > > --- > > > > fs/binfmt_elf.c | 23 +++++++++++------------ > > 1 file changed, 11 insertions(+), 12 deletions(-) > > > > --- a/fs/binfmt_elf.c > > +++ b/fs/binfmt_elf.c > > @@ -93,7 +93,7 @@ static int elf_core_dump(struct coredump_params *cprm); > > #define ELF_CORE_EFLAGS 0 > > #endif > > > > -#define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1)) > > +#define ELF_PAGESTART(_v) ((_v) & ~(int)(ELF_MIN_ALIGN-1)) > > Errr, this I don't like. I assume this is because of the min() use > below? Yes, this is to shut up the warning. The macro is slightly incorrect because "_v" can be either uint32_t or uint64_t. But standard ALIGN macros are slightly incorrect too. I don't want to clean this particular mess right now. Those are separate stables. > > #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1)) > > #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1)) > > > > @@ -399,22 +399,21 @@ static unsigned long elf_map(struct file *filep, unsigned long addr, > > return(map_addr); > > } > > > > -static unsigned long total_mapping_size(const struct elf_phdr *cmds, int nr) > > +static unsigned long total_mapping_size(const struct elf_phdr *phdr, int nr) > > { > > - int i, first_idx = -1, last_idx = -1; > > + elf_addr_t min_addr = -1; > > + elf_addr_t max_addr = 0; > > + bool pt_load = false; > > + int i; > > > > for (i = 0; i < nr; i++) { > > - if (cmds[i].p_type == PT_LOAD) { > > - last_idx = i; > > - if (first_idx == -1) > > - first_idx = i; > > + if (phdr[i].p_type == PT_LOAD) { > > + min_addr = min(min_addr, ELF_PAGESTART(phdr[i].p_vaddr)); > > + max_addr = max(max_addr, phdr[i].p_vaddr + phdr[i].p_memsz); > > How about: > min_addr = min_t(elf_addr_t, min_addr, ELF_PAGESTART(phdr[i].p_vaddr)); > max_addr = max_t(elf_addr_t, max_addr, phdr[i].p_vaddr + phdr[i].p_memsz); No! The proper fix is to fix ELF_PAGESTART().