Received: by 10.192.165.148 with SMTP id m20csp470998imm; Fri, 4 May 2018 00:44:41 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpSDPjw4DJr/i1cstOxDOj/z16+uP0LVyXexT1+ILQps3ZoN+vHq1Kpf4ZT7m39lPJ7ofIM X-Received: by 2002:a17:902:4003:: with SMTP id b3-v6mr26274205pld.15.1525419881672; Fri, 04 May 2018 00:44:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525419881; cv=none; d=google.com; s=arc-20160816; b=eNoV1gjo6TWebFhbZvSJaqycz7lsqLIPLHjfBpLRk8Wl2R6xNYYsxquKp1yaYKem7a VZ4QyRomwm1siSJ23wRKRZSzj6w7/YsGgkXQb0peyoO9NG8t3CcKGe8IMuxzMlinzh6w sndqq6njTJX1kJPmNwkZWjdWKUMVWcvtKUD4rWeZHjG/WJSHUGtXsmLKQxEWggvYwOtq 6ZWpkWZ3kAzsij7iXLPSXe8xRejL76JhYszKCkaOsa/IrFqNlsRFyOrNQyRAHoQJdZ8Z UQldcJwUAyqKYoKCbm6IZH+xAoYYDsLqyub2BtTD0CXONipDd9sG+SuNEtD+hJneo/dw gwqw== 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 :in-reply-to:references:mime-version:dkim-signature :arc-authentication-results; bh=Z0o0KrYqpkJR2z5uQGsNv301Xp4SL1KXpu5CAexeZ/M=; b=KQQu9Y9Bsf/A1+GIWP97aCIK7AHiTXSK2+wWtETEcgh1fBkfNgcn2LnDcwP6xlBh5y qS7nmr+t3aBnOjtdLLcKlSSP4RnucI/HzqCFMH79wI5Hk3iEMPyYibo69dFT6uI23f2Y iTOOyClSnKz3BvE5Xb0JcHuV5LWOespUc+hFhOIDwXG+ONq6kxTBnXy4GPP/g6iMVVhi 8ahJkdd8jMI6lB1GrK0IutEzUCd8PSf9s6xqVcE3dW4/qYESggvXUuhl+PP1Mm9LKCgy kIS/PF8iBK3FMomaCBBDJ2+ke3FL6VnLKrVYfOCSdiOLfRNaOYNXWLpxy7hGui7uXSa7 Afow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=W0937OPF; 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 b21-v6si12635458pgn.276.2018.05.04.00.44.27; Fri, 04 May 2018 00:44:41 -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=@linux-foundation.org header.s=google header.b=W0937OPF; 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 S1751411AbeEDHnH (ORCPT + 99 others); Fri, 4 May 2018 03:43:07 -0400 Received: from mail-it0-f65.google.com ([209.85.214.65]:38865 "EHLO mail-it0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750707AbeEDHnE (ORCPT ); Fri, 4 May 2018 03:43:04 -0400 Received: by mail-it0-f65.google.com with SMTP id q4-v6so2336509ite.3 for ; Fri, 04 May 2018 00:43:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Z0o0KrYqpkJR2z5uQGsNv301Xp4SL1KXpu5CAexeZ/M=; b=W0937OPFmoCybHraq7wX8mBBeWSCwjDFejXRqqduA18riYJ2IFgWPhBRsYMskMxA5x fA49zgXGNT7ZLhg6WpEez607oYjRhiJopGbYrPEBWCTvko1drEmh5AHzO25PAdKGcM1m NES4YO0UVgoN2CrzxDRjTAJCeKN5S467vcjAk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Z0o0KrYqpkJR2z5uQGsNv301Xp4SL1KXpu5CAexeZ/M=; b=dpQUPMviHhLodEEDm9rSsPIg3WINSONSQJ4aPbI7v7gSFTBfbxgopjBsP/fYQzDdOX KVHcXXVsWFYyLDkEiMvW4Y3tP595Md2jy/5Lg4f0Pw2zKjoJzjTelzfegBd12yTOuePd fRqtVfzZ5TVSDKE/yirhCR7cMKPoa3VPBHq0KvhNQNhoxKQws5CI2AFdwSi6x3Xx/lFL Hu67x5Z8o2+JGxWtXYmcQblcGe2miIPpg2jiH4JhiAPxQtT3vQ+PeaD6LUHfFnUoc5PE b9bNtsyTmCf+9XSa6Jmui5AZRStlyzOsInMvbSrQC8ReWHPDohWg1HYwOA6sq6UtdMrR GX8g== X-Gm-Message-State: ALQs6tDU7DGmcb1Tiea6XgZTk3Y14UY7q1DGnvhC3mheA3lkaAWkauq3 ul1a6A/0/MORBP+TVYSJAj16/aoTlMUr1qcJvPwiKg== X-Received: by 2002:a24:21ca:: with SMTP id e193-v6mr26851671ita.108.1525419783397; Fri, 04 May 2018 00:43:03 -0700 (PDT) MIME-Version: 1.0 References: <20180214182618.14627-1-willy@infradead.org> <20180214182618.14627-3-willy@infradead.org> In-Reply-To: <20180214182618.14627-3-willy@infradead.org> From: Linus Torvalds Date: Fri, 04 May 2018 07:42:52 +0000 Message-ID: Subject: Re: [PATCH 2/2] mm: Add kvmalloc_ab_c and kvzalloc_struct To: Matthew Wilcox Cc: Andrew Morton , Matthew Wilcox , linux-mm , Kees Cook , Linux Kernel Mailing List , 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, Feb 14, 2018 at 8:27 AM Matthew Wilcox wrote: > +static inline __must_check > +void *kvmalloc_ab_c(size_t n, size_t size, size_t c, gfp_t gfp) > +{ > + if (size != 0 && n > (SIZE_MAX - c) / size) > + return NULL; > + > + return kvmalloc(n * size + c, gfp); Ok, so some more bikeshedding: - I really don't want to encourage people to use kvmalloc(). In fact, the conversion I saw was buggy. You can *not* convert a GFP_ATOMIC user of kmalloc() to use kvmalloc. - that divide is really really expensive on many architectures. Note that these issues kind of go hand in hand. Normal kernel allocations are *limited*. It's simply not ok to allocate megabytes (or gigabytes) of mmory in general. We have serious limits, and we *should* have serious limits. If people worry about the multiply overflowing because a user is controlling the size valus, then dammit, such a user should not be able to do a huge gigabyte vmalloc() that exhausts memory and then spends time clearing it all! So the whole notion that "overflows are dangerous, let's get rid of them" somehow fixes a bug is BULLSHIT. You literally introduced a *new* bug by removing the normal kmalloc() size limit because you thought that pverflows are the only problem. So stop doing things like this. We should not do a stupid divide, because we damn well know that it is NOT VALID to allocate arrays that have hundreds of fthousands of elements, or where the size of one element is very big. So get rid of the stupid divide, and make the limits be much stricter. Like saying "the array element size had better be smaller than one page" (because honestly, bigger elements are not valid in the kernel), and "the size of the array cannot be more than "pick-some-number-out-of-your-ass". So just make the divide go the hell away, a and check the size for validity. Something like if (size > PAGE_SIZE) return NULL; if (elem > 65535) return NULL; if (offset > PAGE_SIZE) return NULL; return kzalloc(size*elem+offset); and now you (a) guarantee it can't overflow and (b) don't make people use crazy vmalloc() allocations when they damn well shouldn't. And yeah, if somebody has a page size bigger than 64k, then the above can still overflow. I'm sorry, that architecture s broken shit. Are there cases where vmalloc() is ok? Yes. But they should be rare, and they should have a good reason for them. And honestly, even then the above limits really really sound quite reasonable. There is no excuse for million-entry arrays in the kernel. You are doing something seriously wrong if you do those. Linus