Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp238038pxb; Mon, 13 Sep 2021 18:01:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyiKL0tjCwxLkIZJKQnrmZPEjgbyf44hyzAHoBYnv9LwuIxV2ThQRIKSyoPvSR6A8+ppvVo X-Received: by 2002:a5e:a913:: with SMTP id c19mr11113435iod.31.1631581289691; Mon, 13 Sep 2021 18:01:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631581289; cv=none; d=google.com; s=arc-20160816; b=MlvM+fhggIeXgi/+dYRClml+IJY8m6klDzZvgr6hV8HE+IhT7szNqUfCK7kAF5jedX 4DhDd3/yahPqiIS5+aPXBvqetaleFntQ0XQYSJwr4ge/6FPo9kTIbOc5u29JDuPCumDF VipwSZJeXmw8KV0hFzZ6w0n+4sf1S9Lz4jescZ0/51+O/+7q7eTqsmRtqARO0kvZmk08 mxNm611LOxNHYmnUGudzukEECgk4h4wq04dkuS4ZJR4Mk9mL/WxANLxp1jd9JIv3iW2f R9+dTXOZZ1UEo/34I0goa8s8cARQXznZV+GD5G0qEvAIsDLt1GT11OCwje4E1KzNgASv scJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:reply-to :in-reply-to:references:mime-version:dkim-signature; bh=TWTMVvhvJotwJPk/ze26bZ/qP+vFniaofcsTjFnegRQ=; b=rSEvEVSON4nWj93+xlB5RWot7ad6X1xeN+TdyRueUPjDRDyN7N2PbVnKzTMMdj6S6T 9ZC6qtIAGEJngKkH6z/CwdnEvEDVu3UruSPk1lMgiCJv7KPkPq+NC5qOAufKQtF4nyvs J1bgmfamYzbryfoKTojzPUXpYNLvWVPPYs+jaVxjpVJp9+csHW5d4gDTWX2p+Epggs8K Z1XpQ4QMQoCozjMEmD9x5KpfQGZBO+cwMyXKBuhwVoPSREjA1v8vuEQMrwG3hShPwa72 ceOpdvuXHy3hRxJY97sZlK50gHVB31cUZSNDwKdmb8F5n8bmLpNargpCNytoBUUVauUU lDDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="C39pM3/I"; 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 c39si7602644jaf.12.2021.09.13.18.01.18; Mon, 13 Sep 2021 18:01:29 -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="C39pM3/I"; 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 S242889AbhIMT7C (ORCPT + 99 others); Mon, 13 Sep 2021 15:59:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242202AbhIMT66 (ORCPT ); Mon, 13 Sep 2021 15:58:58 -0400 Received: from mail-il1-x12f.google.com (mail-il1-x12f.google.com [IPv6:2607:f8b0:4864:20::12f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65EF7C061574; Mon, 13 Sep 2021 12:57:42 -0700 (PDT) Received: by mail-il1-x12f.google.com with SMTP id i13so11406148ilm.4; Mon, 13 Sep 2021 12:57:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:reply-to:from:date:message-id :subject:to:cc; bh=TWTMVvhvJotwJPk/ze26bZ/qP+vFniaofcsTjFnegRQ=; b=C39pM3/I8tOoAoI7Wu65UGq1fOtrlr0vE4t+ukOt6bYdNi5dPCXqr6X+Zb8O69IwfU x25yCGUAwf8TJdmGPJ8YViTEbCVawWcjq5skN9JR/kuJW1bfv0HQqzOdisiitLZwJEgI ZnJ4x6p8KnDuHZ2D97B0ESOA3q0fLVrO75bhx+cY0S9bP5ZSboc3oWtEOXFo2lp7qVqt alBTz95njw8gAfu4ncrxm97wVs+t8lC66ekuXJvLKpcnIzBlDh7Gf4nDf/xCAcL6+GEx V7zSGqKeUaYw9BgeDEL1+OOCiUkLqI0KCW78SxVPo0VodZnw0zG+UCLVbm1MQlBclE0u JGwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:reply-to :from:date:message-id:subject:to:cc; bh=TWTMVvhvJotwJPk/ze26bZ/qP+vFniaofcsTjFnegRQ=; b=7tTPCMY5fPUiy9MuNGtOFwLr7Eqj/U3iA2z5KltK6fWXkbGcccPLRFjdN6ZSDHpr6n DdivJtWP/xbmqu86d5puUBB84c4lDe5OeK3XyHyGJZVlYloKZSCGePyUMo/58J0pE6yU 4MfUm7PRJXRrzI3rx6edIUiddEJ3xyzAkn//lH1rUs9pSm0Xt1LJXBCZAwOHFoF+YIUL ex6xrrZZXMNMZbBFKeQ0rr/8+49cdmWzJlCg1N/lxTU893xtTy4yuXI9NKlX32Fr2asT v6y8iPZEfk+3gFYA7PVdMfv4cXDhcYHkfJyI5pfPxQwVtjE2KWSYgSfWKT0PP/i6Q+cG spiw== X-Gm-Message-State: AOAM532AEbs20paF9fHAFl9yBKk5UVmT702DSD+Pfc6hm3TSXBt6M2Rd VpqEbEm0MaLaK6+yPwggMsKgrK2qtkQEgzueDT38pHDQ/ZA5lA== X-Received: by 2002:a05:6e02:ee1:: with SMTP id j1mr9357308ilk.61.1631563061774; Mon, 13 Sep 2021 12:57:41 -0700 (PDT) MIME-Version: 1.0 References: <20210913131113.390368911@linuxfoundation.org> <20210913131114.028340332@linuxfoundation.org> In-Reply-To: Reply-To: sedat.dilek@gmail.com From: Sedat Dilek Date: Mon, 13 Sep 2021 21:57:05 +0200 Message-ID: Subject: Re: [PATCH 5.14 018/334] nbd: add the check to prevent overflow in __nbd_ioctl() To: Nick Desaulniers Cc: Greg Kroah-Hartman , Linus Torvalds , Naresh Kamboju , Nathan Chancellor , Baokun Li , open list , linux-stable , Hulk Robot , Josef Bacik , Jens Axboe , Sasha Levin , clang-built-linux , lkft-triage@lists.linaro.org, llvm@lists.linux.dev, Kees Cook Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Sep 13, 2021 at 9:53 PM 'Nick Desaulniers' via Clang Built Linux wrote: > > On Mon, Sep 13, 2021 at 11:39 AM Nick Desaulniers > wrote: > > > > On Mon, Sep 13, 2021 at 10:58 AM Greg Kroah-Hartman > > wrote: > > > > > > On Mon, Sep 13, 2021 at 09:52:33PM +0530, Naresh Kamboju wrote: > > > > [PATCH 00/10] raise minimum GCC version to 5.1 > > > > https://lore.kernel.org/lkml/20210910234047.1019925-1-ndesaulniers@google.com/ > > > > > > Has anyone submitted a fix for this upstream yet? I can't seem to find > > > one :( > > > > That lore link has a series to address this, though that's maybe > > something we don't want to backport to stable. > > > > I thought about this all weekend; I think I might be able to work > > around the one concern I had with my other approach, using > > __builtin_choose_expr(). > > > > There's an issue with my alternative approach > > (https://gist.github.com/nickdesaulniers/2479818f4983bbf2d688cebbab435863) > > with declaring the local variable z in div_64() since either operand > > could be 64b, which result in an unwanted truncation if the dividend > > is 32b (or less, and divisor is 64b). I think (what I realized this > > weekend) is that we might be able to replace the `if` with > > `__builtin_choose_expr`, then have that whole expression be the final > > statement and thus the "return value" of the statement expression. > > Christ...that...works? Though, did Linus just merge my patches for gcc 5.1? > "Merge branch 'gcc-min-version-5.1' (make gcc-5.1 the minimum version)" - Sedat - https://git.kernel.org/linus/316346243be6df12799c0b64b788e06bad97c30b > Anyways, I'll send something like this for stable: > --- > > diff --git a/include/linux/math64.h b/include/linux/math64.h > index 2928f03d6d46..e9ab8c25f8d3 100644 > --- a/include/linux/math64.h > +++ b/include/linux/math64.h > @@ -11,6 +11,9 @@ > > #define div64_long(x, y) div64_s64((x), (y)) > #define div64_ul(x, y) div64_u64((x), (y)) > +#ifndef is_signed_type > +#define is_signed_type(type) (((type)(-1)) < (type)1) > +#endif > > /** > * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder > @@ -112,6 +115,15 @@ extern s64 div64_s64(s64 dividend, s64 divisor); > > #endif /* BITS_PER_LONG */ > > +#define div64_x64(dividend, divisor) ({ \ > + BUILD_BUG_ON_MSG(sizeof(dividend) < sizeof(u64),\ > + "prefer div_x64"); \ > + __builtin_choose_expr( \ > + is_signed_type(typeof(dividend)), \ > + div64_s64(dividend, divisor), \ > + div64_u64(dividend, divisor)); \ > +}) > + > /** > * div_u64 - unsigned 64bit divide with 32bit divisor > * @dividend: unsigned 64bit dividend > @@ -142,6 +154,28 @@ static inline s64 div_s64(s64 dividend, s32 divisor) > } > #endif > > +#define div_x64(dividend, divisor) ({ \ > + BUILD_BUG_ON_MSG(sizeof(dividend) > sizeof(u32),\ > + "prefer div64_x64"); \ > + __builtin_choose_expr( \ > + is_signed_type(typeof(dividend)), \ > + div_s64(dividend, divisor), \ > + div_u64(dividend, divisor)); \ > +}) > + > +// TODO: what if divisor is 128b? > +#define div_64(dividend, divisor) ({ > \ > + __builtin_choose_expr( > \ > + __builtin_types_compatible_p(typeof(dividend), s64) || > \ > + __builtin_types_compatible_p(typeof(dividend), u64), > \ > + __builtin_choose_expr( > \ > + __builtin_types_compatible_p(typeof(divisor), > s64) || \ > + __builtin_types_compatible_p(typeof(divisor), > u64), \ > + div64_x64(dividend, divisor), > \ > + div_x64(dividend, divisor)), > \ > + dividend / divisor); > \ > +}) > + > u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder); > > #ifndef mul_u32_u32 > --- > -- > Thanks, > ~Nick Desaulniers > > -- > You received this message because you are subscribed to the Google Groups "Clang Built Linux" group. > To unsubscribe from this group and stop receiving emails from it, send an email to clang-built-linux+unsubscribe@googlegroups.com. > To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/CAKwvOdmCS5Q7AzUL5nziYVU7RrtRjoE9JjOXfVBWagO1Bzbsew%40mail.gmail.com.