Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp1628138pxb; Thu, 7 Oct 2021 11:34:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwakVkIC9yAZ7XRfwnaIzCLXr6SnhvUT2Z4LBU3rCvhUgOzUjJZQFsKiHF8mPJ6BI4HrLqd X-Received: by 2002:a63:8c42:: with SMTP id q2mr942823pgn.325.1633631643026; Thu, 07 Oct 2021 11:34:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633631643; cv=none; d=google.com; s=arc-20160816; b=Kj0dVFrw/BLTRMsH33pYb5qlX49Q7UanAr4xxHRIBdCIg1SYLB1GQnQiZJHIXnacpz UtJAZnUxrWafIOnY+HgpqOp6i8DQtDQYMVv72z+fP5Gy3BPpGyolkA6u/M2FL/nNKikh JZhwjD0IN5oICxrvHjb2F2zuF14tXMWaAMkPi19Pw/mH8XP7Puzfri7WgF/Rg/FRocXU Kw4tq1wEr/ZEZZBI0E0B9s4L7XhG2WgcpSBxIhqjHzL4bvP905lqP8IVy4SeyjCV/xJP NOhxELaXA4v/JwfJ44zeSnxN+PNH84HDT+J9+8cuVJQdMPZoRQk7COapYiTegOHuZ1af gJIg== 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=6LwfThH1T+dtzaaoOR7FbBXxaA+5ZtH4LGikgiOctTM=; b=mgr5aCxCoBPOQl0RBZkfsUUHIE5rL8LxsePttjoGjLcmepEn4com68FmMsvxQNswCC MtkbpcCO7gtUgdPYBaV4E0lO9lMmdfSPLaz4i+TAVZB4qH+HriGsNffI54Q0i6ABZLx5 JLbJvsRt5t6NSrtjl31Fs/p8TUgv2xcuAWdMl7CNgm7Vnf6M+Jaq0fxYucyk8tm6DRox nwbwteI2KhJMk+1Q0kWyf/YSSWqLC11S+TXJlIAmAj1kwt2uJt+en8zAE3qLzbR7Iue7 2hg7oiUegsZFcuY+ECXatQpn/4+BoDKc5a61ZO6lRfCEnKv2C5HcQ6b4UzrUvEJu3QwU d/IA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="Fbys/hX/"; 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 l9si10274846pjc.80.2021.10.07.11.33.49; Thu, 07 Oct 2021 11:34:03 -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="Fbys/hX/"; 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 S233843AbhJGSdh (ORCPT + 99 others); Thu, 7 Oct 2021 14:33:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231340AbhJGSdh (ORCPT ); Thu, 7 Oct 2021 14:33:37 -0400 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F823C061570 for ; Thu, 7 Oct 2021 11:31:43 -0700 (PDT) Received: by mail-ed1-x52d.google.com with SMTP id y12so13312444eda.4 for ; Thu, 07 Oct 2021 11:31:43 -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=6LwfThH1T+dtzaaoOR7FbBXxaA+5ZtH4LGikgiOctTM=; b=Fbys/hX/KwxhLeHeUdym/Ow/lT3k4R9FyxvX7TI52UOEaF/lUT6EgKzEEiBn2vdXqt b6wVZd9pfZBiJrs6sAjcFXS/oCEjyXrDRT3lv+80P6U4/fkKZOy4qxGl2RHrjgPwYv3w e7ie+Zlh4MgXgLLfKAi+8FH56XCjt6FpeR+bgDjrgDX/gaEp6GBy7rnNPs/bsUiP/GJz RRwm9QRYUuzLLw0LAW77yfIgr/iOjFztj80PAvi0ex7Vd7fNiejBhe+EnLaU/M4JrKTu d7ruN+4kySzmPJERzCXelSFM0hcU7QhZFeIsWR5mA8GJVpse7+i86AzFunGasRraZqdV QFKg== 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=6LwfThH1T+dtzaaoOR7FbBXxaA+5ZtH4LGikgiOctTM=; b=gG0USRpxcnFOIdRxgxduiv/AZoGvRLl5GCsQByHb3x/wc4K0VrVZmB55hvFlpcn0Wh GgxOSRkIhDciHhI5zgNb3tu5YXEM0pu45H5qb0SbgGxTdPzHnCe7ADWhvDSaWer2pU1w WC0jj5A8jQHQETmXyptIDab8V4Horwh9JTBCC0qZiV314klRjYLhHSH2fP2UvAYcYgve um7tzdqnmx4tsgi+itqDn0dN4rXT+zFcvbxl6NA6NfkPmsrMpAQcWN0eQttqK9cmydy3 FJr8BoYsEdxrAn/gbkgYPSYEwc9I6V/U1QMPYqIg2++3f0uh7GYOTUqj2f32xFEraeiU nXOg== X-Gm-Message-State: AOAM531knDl2pqNKj9J2bvIHd111LJFoVQWgEfnvZec3g7VJ+K9a2PEc 7shLuLqi1zUsPyJVNo10tg== X-Received: by 2002:a05:6402:280b:: with SMTP id h11mr7266098ede.78.1633631501906; Thu, 07 Oct 2021 11:31:41 -0700 (PDT) Received: from localhost.localdomain ([46.53.254.50]) by smtp.gmail.com with ESMTPSA id v13sm67464ejh.62.2021.10.07.11.31.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Oct 2021 11:31:41 -0700 (PDT) Date: Thu, 7 Oct 2021 21:31:40 +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> <202110071038.B589687@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <202110071038.B589687@keescook> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 07, 2021 at 10:40:01AM -0700, Kees Cook wrote: > On Thu, Oct 07, 2021 at 08:20:03PM +0300, Alexey Dobriyan wrote: > > 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. > > Right, but "int" is neither 64-sized nor unsigned. :P I would just leave > this macro as-is. "int" will be promoted to either "unsigned int" or to whatever 64-bit ELF type is, it is enough to fix warnings, it will be sign extended correctly.