Received: by 10.192.165.148 with SMTP id m20csp231093imm; Wed, 9 May 2018 11:40:04 -0700 (PDT) X-Google-Smtp-Source: AB8JxZr/0k8ACvfDBd5Q2s3wqY8PT5O3SYGC/Wm8GmGbn/dmpu0E7f4w7cXRfSzKpR0FjnKPaYld X-Received: by 2002:a17:902:7841:: with SMTP id e1-v6mr47149272pln.197.1525891204329; Wed, 09 May 2018 11:40:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525891204; cv=none; d=google.com; s=arc-20160816; b=yTnqlJ5zBw7xRFjowOwcPVB7h4wgr0W3hCYEl9mvRGfdadFBpUTmXRqsnvfs5TMNWq Qv/WQg5Wpl85nKo62VPLvDOSm+kGscPrZVrZ0y3mU2yR4vRVjZ4n1WmfilUeLBlrV1uy 4+6p8qM7ivlojMBqqdjmv3R2qtgdlN7UW3fm4WOktBJ9d35oPYbDh30OA9AMO7lu9dKK lmjxQdhezRICXClWUJhKeHZBKlmD+Q+RD52APaVx66/vjG663ywyMXbJYykCIW7jTDIM EAC7ESvgy2QNATyiYpIL0FY23kxu3OtNru4oZhrB7A3W04gjeJR12Zi0dMImzxDqYy7+ 2ytw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature :arc-authentication-results; bh=jx6a3gva42mP2fXmj7HlIz0gtk+TEBHnPHYJRtQjMpU=; b=Xb2eJsf5XBWc5y1gcjWS4c78NjBsQoEdd+LKH3un3CyblWuVvkDmXOMLU4LB6kz6ui 3x6YR8ySw3fNTqInESn7zDMYHwJ4fkO8qhYW7ukmbSyYpWM4/ZccT2hFk6JMiedc9WxH uTF7S6Y3y4WUhRNT7lHLRVgQXd4ynwnQb+9ehqwj8dQFVHfq7nqqkkfhdPdVV8rpHPyj htTh3MKMKfeBEHhjtPE3UUy1aClcOuAVMowu+ZxzSrbeIHzW/P08qBi0kv08WvCD1Zxk XZu1ASPqq3cM35OELhLbuRtkAI5+ahR+B/dYo1kClrubRQY+defuWkCqQOgOWUfUvhXd 1Kng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=f1gfpW4o; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f5-v6si21751330pgn.459.2018.05.09.11.39.49; Wed, 09 May 2018 11:40:04 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=f1gfpW4o; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964827AbeEISjj (ORCPT + 99 others); Wed, 9 May 2018 14:39:39 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:50480 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755415AbeEISji (ORCPT ); Wed, 9 May 2018 14:39:38 -0400 Received: by mail-wm0-f65.google.com with SMTP id t11-v6so152642wmt.0 for ; Wed, 09 May 2018 11:39:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=jx6a3gva42mP2fXmj7HlIz0gtk+TEBHnPHYJRtQjMpU=; b=f1gfpW4oLXVugKswbZs9JypUkpLIXhHYDiv34Fji/XwOohvHjLgywJ+Z/zAIu8vZeB agNFTAmF5Ywd66L0WUH0ZyYiMclrQ0RfRGmHwSlR3F60dyvNxuweGZJX+7/6VzVEbEqo 6CpjGci3HCVaJngYcV6hSrwlMJed5H7RRz4rI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=jx6a3gva42mP2fXmj7HlIz0gtk+TEBHnPHYJRtQjMpU=; b=pePFn5oEl72dRPQyaPST/sakGRoNqlxXiUX0xMg7graXjOcSAm1p/z9o+WtYMaF7WO TwjnrdlGsa8S7MMnV0hOqOpZ2RVbnVI7yh8HpIqTIrfCfUOFRr1sdA6q8jFbAbBckyKQ iesc5Qgd6TLPKB1oTU640BKHPWFaRizygROuxgIcjZYGhd1YdiJ+szvywPCzCRyWrBna s+WrZXoPHMDAiLjlgcyzdScRfiVWhgzhdhPF681JfMv0EBCZGOsJdB6oTluwH0mgguZ2 ea4Cts1e+C4BQej6Q2ndhtgXNNx0JESvWZIYC/bBHEmRHEak0WYvI1pk7/Zp2n1VS5cX 8T7g== X-Gm-Message-State: ALQs6tCokw7Yq2/WUHMAEQqMAjsBTNUjFl0075D6tiMbEW+tae+DVqZg xfWLOLpmgqTWn6nKFK5g07QBnA== X-Received: by 2002:a50:b384:: with SMTP id s4-v6mr47947030edd.28.1525891177035; Wed, 09 May 2018 11:39:37 -0700 (PDT) Received: from [192.168.0.189] (dhcp-5-186-126-104.cgn.ip.fibianet.dk. [5.186.126.104]) by smtp.gmail.com with ESMTPSA id a2-v6sm12877507edn.25.2018.05.09.11.39.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 May 2018 11:39:36 -0700 (PDT) Subject: Re: [PATCH 04/13] mm: Use array_size() helpers for kmalloc() To: Kees Cook Cc: Matthew Wilcox , Matthew Wilcox , LKML , Linux-MM , Kernel Hardening References: <20180509004229.36341-1-keescook@chromium.org> <20180509004229.36341-5-keescook@chromium.org> <20180509113446.GA18549@bombadil.infradead.org> From: Rasmus Villemoes Message-ID: Date: Wed, 9 May 2018 20:39:35 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2018-05-09 20:07, Kees Cook wrote: > On Wed, May 9, 2018 at 11:00 AM, Rasmus Villemoes > wrote: > Okay, consensus is to remove new SIZE_MAX checks, then? Yes, don't add such to static inlines. But the out-of-line implementations do need an audit (as you've observed) for unsafe arithmetic on the passed-in size. >> With __builtin_constant_p(size) && size == SIZE_MAX, gcc could be smart >> enough to elide those two instructions and have the jo go directly to >> the caller's error handling, but at least gcc 5.4 doesn't seem to be >> that smart. So let's just omit that part for now. >> >> But in case of the kmalloc_array functions, with a direct call of >> __builtin_mul_overflow(), gcc does combine the "return NULL" with the >> callers error handling, thus avoiding the six byte "%rdi = -1; jmp >> back;" thunk. That, along with the churn factor, might be an argument >> for leaving the current callers of *_array alone. But if we are going to >> keep those longer-term, we might as well convert kmalloc(a, b) into >> kmalloc_array(a, b) instead of kmalloc(array_size(a, b)). In any case, I >> do see the usefulness of the struct_size helper, and agree that we >> definitely should not introduce a new *_struct variant that needs to be >> implemented in all families. > > I'd like to drop *calloc() and *_array() to simplify APIs (and improve > developer sanity). Are you suggesting we should not use the overflow > helpers in kmalloc_array(), instead leaving the existing open-coded > overflow check? No, quite the contrary. I suggest using check_mul_overflow() directly in kmalloc_array (and by implication, kcalloc), and also all other *_array or *_calloc that are static inlines. That's separate from converting kmalloc(a*b) to use some safer variant, and should not be controversial (and can generate better code for all the existing callers). Now, what kmalloc(a*b) should be converted to is a question of the long-term plans for *_array. If you want to remove it completely, eventually, it doesn't make sense to coccinel (yeah, that's a verb) in new users. And a third question is whether and when to mechanically change all (pre-)existing kmalloc_array() into kmalloc(array_size()). I don't have an opinion on the latter two. Rasmus