Received: by 10.192.165.148 with SMTP id m20csp193155imm; Wed, 9 May 2018 11:00:23 -0700 (PDT) X-Google-Smtp-Source: AB8JxZovppgRmJKVeKbiNdA13py7/EXgV4gDdEcxTQm4Dvek9acVJ8SrVw98pOlDt+gtSz/ivAlU X-Received: by 2002:a65:590e:: with SMTP id f14-v6mr36801253pgu.282.1525888823575; Wed, 09 May 2018 11:00:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525888823; cv=none; d=google.com; s=arc-20160816; b=yHEsEFLmPniLBPeUS9a6OuHIM44s9fVYasRBNw7WtivNOwzhOMx4KQnmsdW8uHQ62l kEfoQaDnDG5/dQlkLHsoEyhvuvSUmi9WQ6mGNt9NLIT57Xp4TAUO6m5o/1370rRbvfRJ 1YQ7Vncr8GCjjGBHPu3BjHBnLBETB1H9PzpPQV3pTTHKSYUBDORyAENJC5V18X+oZ3Da ZDuhjcN0G6+xHe54apsvh7mdOJmWTZlgjCHw/leQzlPXbTnlVX6wiiD+uZv2+wfh5eqX kW/5cMYHDrFvygfi1uM7LpsOBRENXcY05lWCj85TcQ+HAOddoHX0M6GE10pEhvjj0/46 x/0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature:dkim-signature :arc-authentication-results; bh=DQPR/2UEti3he7HEuIQdzjcbIMZwr5B7sgkfaXYi8rE=; b=OX4IjBgBg2aztPjPv2U4NcIXUeg/Onv0IPC47FIXtn+wX/tNA3rwewOAfZW48RWwKU VSrMPXgT3rqKByJKPtWwLFKGRvxfRsk8SO02qE9r1yQLlYLcU/3adxCxm6SZV5p7AlE0 OmOLjmcXLlR97I5GBbJXQhLyk5TZtlS5vHkPDjAcPySHGxh7mMvV9MunhFvKzwrKOugM dLQ41Q3IDebnPzyb6X+e7UVD1bOasozd0cK3+bKI5Ihjz/ROCKKkTYxQguTDpxs1FL75 1P2C9IuuYnfeeowBxu7O2S8ivkOtNqT4N5FjVY8sR36VGY+mpbf8R/yYWwJiGwPS37U5 avIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@google.com header.s=20161025 header.b=UKRGB2DN; dkim=fail header.i=@chromium.org header.s=google header.b=NjY398o2; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l5-v6si10365584pgp.239.2018.05.09.11.00.06; Wed, 09 May 2018 11:00:23 -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=fail header.i=@google.com header.s=20161025 header.b=UKRGB2DN; dkim=fail header.i=@chromium.org header.s=google header.b=NjY398o2; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935275AbeEIR7B (ORCPT + 99 others); Wed, 9 May 2018 13:59:01 -0400 Received: from mail-vk0-f66.google.com ([209.85.213.66]:35421 "EHLO mail-vk0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935147AbeEIR7A (ORCPT ); Wed, 9 May 2018 13:59:00 -0400 Received: by mail-vk0-f66.google.com with SMTP id g72-v6so17959170vke.2 for ; Wed, 09 May 2018 10:58:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=DQPR/2UEti3he7HEuIQdzjcbIMZwr5B7sgkfaXYi8rE=; b=UKRGB2DNLGfTiD3Q1Ro8cfIfBzIDm4ifVM3eCpot31lNiZsqFfC9uwZwjVWHWInfLd S6GJqk9gFMV8o3xjt1qeLxgxXXk3FyboVIjWij6RiyQVwDZ9yPEpcu9ytkRIgfD/xYKn MpioROt8E4G0rWT1fOonnaERxM1BpWtFcRiK5V2gUhVTJgI+ujX6iX/kHNm9VqA9At9l CMZ1xr80N127YGeRH07I5DbrULlNUofQ8nWAXka2dV4ZMKtZDi22YrK9Xb/xI8bu2G0h XSDUpdPII6MurjtVuyGi6b8eGs6xDb4cGm7C6Dg9ajPzS02mOFdfoKUzltTdBXJ9d+oh l97A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=DQPR/2UEti3he7HEuIQdzjcbIMZwr5B7sgkfaXYi8rE=; b=NjY398o2CVsJMgLu9MiPRPRwXNwikwcDBO3/eUFj044P3C3aAQ159w7i7nWQ3OQeWe 5syMk7z3g6vaEMYf0caHPlQNlYKlBtJ3iUKEk5e2kRn2iht0pGfhJRiI8uIGj2Xa6K0B 7cXlwVyBDpAdOEN2BPshOJgeRIRTx5k5KQzMg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=DQPR/2UEti3he7HEuIQdzjcbIMZwr5B7sgkfaXYi8rE=; b=pH/g0WCYs16qo0XW2zRAU2SgOxBqhaGGkW/TO/OvHnujyW5JOgT9Djg6noknHUiZkV QsHpUZWCi5TGY6LzjnxT4+95kG+7tI1QQDRcdTZNKlmEp8d39LLQkuPcB4p6B8nC8jgT Ra6l1/8ghx/3zhqCxuTQOTOAG/6RSoix1DbF2cMObiHhQdDnYLU7x68577CiQxg6oFdm awXOt0T++GzYDKUnoK761WU4CSMJY3hggKiqiXt9bnlVK3K4CUHuFFrruDjkMErhXx0F KoeuOLd/KUrYMOAjj9smzuYb9CbOV3shzRdx3ALfoap9Oq2UvNCGcFssLeCDDwCSBJY+ NWgQ== X-Gm-Message-State: ALQs6tBU6InbeFIIli3DIdIGWJJ7LXPu3EhSY+WaqJx5m0ZcPUjx7BxH +d6mfQcNmI/qwYCTrw3D0dqpeKMgkEP40XTjEWNmtw== X-Received: by 2002:a1f:ab47:: with SMTP id u68-v6mr37803857vke.158.1525888738995; Wed, 09 May 2018 10:58:58 -0700 (PDT) MIME-Version: 1.0 Received: by 10.31.11.209 with HTTP; Wed, 9 May 2018 10:58:57 -0700 (PDT) In-Reply-To: <20180509113446.GA18549@bombadil.infradead.org> References: <20180509004229.36341-1-keescook@chromium.org> <20180509004229.36341-5-keescook@chromium.org> <20180509113446.GA18549@bombadil.infradead.org> From: Kees Cook Date: Wed, 9 May 2018 10:58:57 -0700 X-Google-Sender-Auth: CqgmZbHkBH80G4xG-VckBG0gz78 Message-ID: Subject: Re: [PATCH 04/13] mm: Use array_size() helpers for kmalloc() To: Matthew Wilcox Cc: Matthew Wilcox , Rasmus Villemoes , LKML , Linux-MM , Kernel Hardening Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, May 9, 2018 at 4:34 AM, Matthew Wilcox wrote: > On Tue, May 08, 2018 at 05:42:20PM -0700, Kees Cook wrote: >> @@ -499,6 +500,8 @@ static __always_inline void *kmalloc_large(size_t size, gfp_t flags) >> */ >> static __always_inline void *kmalloc(size_t size, gfp_t flags) >> { >> + if (size == SIZE_MAX) >> + return NULL; >> if (__builtin_constant_p(size)) { >> if (size > KMALLOC_MAX_CACHE_SIZE) >> return kmalloc_large(size, flags); > > I don't like the add-checking-to-every-call-site part of this patch. > Fine, the compiler will optimise it away if it can calculate it at compile > time, but there are a lot of situations where it can't. You aren't > adding any safety by doing this; trying to allocate SIZE_MAX bytes is > guaranteed to fail, and it doesn't need to fail quickly. Fun bit of paranoia: I added early checks to devm_kmalloc() too in another patch after 0-day started yelling about other things, and this morning while removing the SIZE_MAX checks based on your feedback, I discovered: void * devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) { struct devres *dr; /* use raw alloc_dr for kmalloc caller tracing */ dr = alloc_dr(devm_kmalloc_release, size, gfp, dev_to_node(dev)); ... static __always_inline struct devres * alloc_dr(dr_release_t release, size_t size, gfp_t gfp, int nid) { size_t tot_size = sizeof(struct devres) + size; struct devres *dr; dr = kmalloc_node_track_caller(tot_size, gfp, nid); ... which is exactly the pattern I was worried about: SIZE_MAX plus some small number would overflow. :( So, I've added an explicit overflow check in devm_kmalloc() now. Thoughts: making {struct,array,array3}_size() return "SIZE_MAX - something" could help for generic cases (like above), but it might still be possible in a buggy situation for an attacker to choose factors that do NOT overflow, but reach something like "SIZE_MAX - 1" and then the later addition will wrap it around. I'm leaning towards doing it anyway, though, since not all factors in a given bug may have very high granularity, giving us better protection than SIZE_MAX. However, since now we're separating overflow checking from saturation (i.e. we could calculate a non-overflowing value that lands in the saturation zone), we can't sanely to the check_*_overflow() cases, since the "saturated" results from array_size() aren't SIZE_MAX any more... I can't decide which is a safer failure case... > Hmm. I wonder why we have the kmalloc/__kmalloc "optimisation" > in kmalloc_array, but not kcalloc. Bet we don't really need it in > kmalloc_array. I'll do some testing. Not sure; my new patches drop it entirely since they're redefining *calloc() and *_array*() with the helpers now... I'll send a v2 shortly (without the treewide changes, since those are huge and only changed slightly with some 0-day noticed glitches). -Kees -- Kees Cook Pixel Security