Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2507455pxb; Mon, 18 Jan 2021 21:34:32 -0800 (PST) X-Google-Smtp-Source: ABdhPJxOnLILcrr6m3J62V5GAuoyRlYDxoswZySCWID6jn1dCH5enNI6KRx9eKXbxpRWlCbS280L X-Received: by 2002:aa7:d511:: with SMTP id y17mr2117067edq.112.1611034472134; Mon, 18 Jan 2021 21:34:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611034472; cv=none; d=google.com; s=arc-20160816; b=OZ1f32B8P+BKNu53z2IAfzwy+tHO6nNk8MqGjjB6OxDFlEt5lrNziYuaveSL/Hd9n7 X1leMrW/auLiPXPn2pkBy2FcggKKDJAjUew0N2nGdFNDYldPuxNXAaGulS/zvpwST/Gf PwFdCY1UYAB9RFTQfEG3oWWEZaWRNib4phe+ryXy1sMlcAZZN0wREW5AtPuE4SldGHFE lilGFQAt0q42w/0bZrodhnqDbymy5qokcVX61CLh6l7fHQQ7USGT/wlVhjbZsC9/VKg2 GFR5vspgBCXcL9V1dlWw5lZ/TCPiOg71HGq6Lp+cO597yy/kghOa6X/eEj/UGFrDPzFG wI9g== 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=Ed3MeSa+vYjkP75JUhCh5CWMiaLqH4Gt6vdQGvVz5Zo=; b=lNOdiL5w1o+D4ia8fMBu82jtALGURSuOKCXwff9nqMJeRThotvUANOJKD5j0DvzU3Z ExvtQwFCnvw7EVy1v90J0S5AeKrsTYpORf4DiECXjW805aEovxNc0/fmHMnLOu4cG1Pm ALkx6OOI9SXsT8uDnapf5x0ZBOakczFKiU8W3jmwVQXh8z9gZdOQU+cbaHImd1Unipds lmopsbiBEDJK1pAJ9cidWRwCP+C463DClcRhixCFFOeI+M2KtUvuxe+92XPv9rUNgByv DmhTP66B1GGPPd8dlbA4KhSLgkt+jVpg05bOOwa5uzVtIi+V34GoF2b3fkEp3G/h6mJr zRxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=ABDcFgQE; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gn23si5652089ejc.12.2021.01.18.21.33.50; Mon, 18 Jan 2021 21:34:32 -0800 (PST) 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=@ziepe.ca header.s=google header.b=ABDcFgQE; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733190AbhARXtJ (ORCPT + 99 others); Mon, 18 Jan 2021 18:49:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726044AbhARXtC (ORCPT ); Mon, 18 Jan 2021 18:49:02 -0500 Received: from mail-qt1-x82c.google.com (mail-qt1-x82c.google.com [IPv6:2607:f8b0:4864:20::82c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 45484C061574 for ; Mon, 18 Jan 2021 15:48:20 -0800 (PST) Received: by mail-qt1-x82c.google.com with SMTP id v5so12543934qtv.7 for ; Mon, 18 Jan 2021 15:48:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=Ed3MeSa+vYjkP75JUhCh5CWMiaLqH4Gt6vdQGvVz5Zo=; b=ABDcFgQEdbegUPy03ChL7qFBRZLE4d5RYB+BvpnNp/pVx70pEavdhXOASQkXNf9bQN h0manBPX6V294ymabO4qrgBYp3em+96lwlU6IePwaAXLM+cGvU+mq3yZi0U+QOkYBEKh GIZqnHRjUw3J4Oxh0Wohqs2W5jyVYzok9Xcp/P5luY0VO19KChzM5IUR1OzhOLwTnul6 lv5qcdCaKVcNYncXHvXGfKlesNbUSeHMjkX1g12IAXyuaZcKDnYdk9sWIaMIx9qPq/pc jDLIyHwfLFRxUtdkfBM3XowK6nVv/q9p9/UfIwRg4HX6hzpZUgNIM4Vrx0+P8z6v1PVv igrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=Ed3MeSa+vYjkP75JUhCh5CWMiaLqH4Gt6vdQGvVz5Zo=; b=svvddnEGem/olnbyojr4BPpbFYXPSl9KUoxdWnbBo21uhRhKJZpzLyehvRjM9630Uk L+WbpLlcrMgVnOpSKCFNCNEwAgk7MIlCWVhYSXIShHDCJITFOXZNbPFgVZi43ycqymPm cCd8p5yzVGXrPERiMpqEs7wZdhxpcm7h+tUK7PzVKeKOzZ63j2kWUNrp3jGY2fWmMcqQ Clp7b6jbI1T7is1MfHPosL+9j/aeWEVC2WEAC64UnO78hplPOaY82QCTgxp7F2HKWtOq TufRDk/vsk9Elco9OdDyL3q+v8Nsgw0mV6WQfnVz9CdIu8UMRxiPj26a3nrGJaVCpiCz a7Aw== X-Gm-Message-State: AOAM532gJ36nRirBTXn/7GnK68SprwO1Fe1QjdWQh20yjnIwOVKtI3Vn aDSxA1gPYkuHTr1zPuuQp/Y4/w== X-Received: by 2002:ac8:4d4d:: with SMTP id x13mr1910197qtv.385.1611013699444; Mon, 18 Jan 2021 15:48:19 -0800 (PST) Received: from ziepe.ca (hlfxns017vw-142-162-115-133.dhcp-dynamic.fibreop.ns.bellaliant.net. [142.162.115.133]) by smtp.gmail.com with ESMTPSA id p13sm11792293qkg.80.2021.01.18.15.48.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jan 2021 15:48:18 -0800 (PST) Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1l1eFu-003P0N-B4; Mon, 18 Jan 2021 19:48:18 -0400 Date: Mon, 18 Jan 2021 19:48:18 -0400 From: Jason Gunthorpe To: Bodo Stroesser Cc: Douglas Gilbert , linux-scsi@vger.kernel.org, linux-block@vger.kernel.org, target-devel@vger.kernel.org, linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org, martin.petersen@oracle.com, jejb@linux.vnet.ibm.com, ddiss@suse.de, bvanassche@acm.org Subject: Re: [PATCH v6 1/4] sgl_alloc_order: remove 4 GiB limit, sgl_free() warning Message-ID: <20210118234818.GP4605@ziepe.ca> References: <20210118163006.61659-1-dgilbert@interlog.com> <20210118163006.61659-2-dgilbert@interlog.com> <20210118182854.GJ4605@ziepe.ca> <59707b66-0b6c-b397-82fe-5ad6a6f99ba1@interlog.com> <20210118202431.GO4605@ziepe.ca> <7f443666-b210-6f99-7b50-6c26d87fa7ca@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <7f443666-b210-6f99-7b50-6c26d87fa7ca@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jan 18, 2021 at 10:22:56PM +0100, Bodo Stroesser wrote: > On 18.01.21 21:24, Jason Gunthorpe wrote: > > On Mon, Jan 18, 2021 at 03:08:51PM -0500, Douglas Gilbert wrote: > >> On 2021-01-18 1:28 p.m., Jason Gunthorpe wrote: > >>> On Mon, Jan 18, 2021 at 11:30:03AM -0500, Douglas Gilbert wrote: > >>> > >>>> After several flawed attempts to detect overflow, take the fastest > >>>> route by stating as a pre-condition that the 'order' function argument > >>>> cannot exceed 16 (2^16 * 4k = 256 MiB). > >>> > >>> That doesn't help, the point of the overflow check is similar to > >>> overflow checks in kcalloc: to prevent the routine from allocating > >>> less memory than the caller might assume. > >>> > >>> For instance ipr_store_update_fw() uses request_firmware() (which is > >>> controlled by userspace) to drive the length argument to > >>> sgl_alloc_order(). If userpace gives too large a value this will > >>> corrupt kernel memory. > >>> > >>> So this math: > >>> > >>> nent = round_up(length, PAGE_SIZE << order) >> (PAGE_SHIFT + order); > >> > >> But that check itself overflows if order is too large (e.g. 65). > > > > I don't reall care about order. It is always controlled by the kernel > > and it is fine to just require it be low enough to not > > overflow. length is the data under userspace control so math on it > > must be checked for overflow. > > > >> Also note there is another pre-condition statement in that function's > >> definition, namely that length cannot be 0. > > > > I don't see callers checking for that either, if it is true length 0 > > can't be allowed it should be blocked in the function > > > > Jason > > > > A already said, I also think there should be a check for length or > rather nent overflow. > > I like the easy to understand check in your proposed code: > > if (length >> (PAGE_SHIFT + order) >= UINT_MAX) > return NULL; > > > But I don't understand, why you open-coded the nent calculation: > > nent = length >> (PAGE_SHIFT + order); > if (length & ((1ULL << (PAGE_SHIFT + order)) - 1)) > nent++; It is necessary to properly check for overflow, because the easy to understand check doesn't prove that round_up will work, only that >> results in something that fits in an int and that +1 won't overflow the int. > Wouldn't it be better to keep the original line instead: > > nent = round_up(length, PAGE_SIZE << order) >> (PAGE_SHIFT + order); This can overflow inside the round_up Jason