From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2D47AC433DB for ; Mon, 25 Jan 2021 14:20:51 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9C3B523102 for ; Mon, 25 Jan 2021 14:20:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9C3B523102 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=i-love.sakura.ne.jp Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 28D658D0002; Mon, 25 Jan 2021 09:20:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 23E5C8D0001; Mon, 25 Jan 2021 09:20:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 155E18D0002; Mon, 25 Jan 2021 09:20:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0113.hostedemail.com [216.40.44.113]) by kanga.kvack.org (Postfix) with ESMTP id F2A678D0001 for ; Mon, 25 Jan 2021 09:20:49 -0500 (EST) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id B8E963635 for ; Mon, 25 Jan 2021 14:20:49 +0000 (UTC) X-FDA: 77744508618.08.woman61_1f0153927586 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin08.hostedemail.com (Postfix) with ESMTP id 68E091819E621 for ; Mon, 25 Jan 2021 14:20:49 +0000 (UTC) X-HE-Tag: woman61_1f0153927586 X-Filterd-Recvd-Size: 5373 Received: from www262.sakura.ne.jp (www262.sakura.ne.jp [202.181.97.72]) by imf16.hostedemail.com (Postfix) with ESMTP for ; Mon, 25 Jan 2021 14:20:48 +0000 (UTC) Received: from fsav403.sakura.ne.jp (fsav403.sakura.ne.jp [133.242.250.102]) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTP id 10PEKgoe081859; Mon, 25 Jan 2021 23:20:42 +0900 (JST) (envelope-from penguin-kernel@i-love.sakura.ne.jp) Received: from www262.sakura.ne.jp (202.181.97.72) by fsav403.sakura.ne.jp (F-Secure/fsigk_smtp/550/fsav403.sakura.ne.jp); Mon, 25 Jan 2021 23:20:42 +0900 (JST) X-Virus-Status: clean(F-Secure/fsigk_smtp/550/fsav403.sakura.ne.jp) Received: from [192.168.1.9] (M106072142033.v4.enabler.ne.jp [106.72.142.33]) (authenticated bits=0) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTPSA id 10PEKfOb081853 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NO); Mon, 25 Jan 2021 23:20:42 +0900 (JST) (envelope-from penguin-kernel@i-love.sakura.ne.jp) Subject: Re: [PATCH v2] mm: memdup_user*() should use same gfp flags To: Michal Hocko Cc: Andrew Morton , Alexander Viro , linux-mm@kvack.org, Sabyrzhan Tasbolatov References: <20210120041843.5090-1-penguin-kernel@I-love.SAKURA.ne.jp> <20210120103436.11830-1-penguin-kernel@I-love.SAKURA.ne.jp> <20210121173538.166e8b27408d4525360fbb70@linux-foundation.org> <5346de21-a404-8476-f2a3-c98c191a2ef9@i-love.sakura.ne.jp> <20210125133244.GK827@dhcp22.suse.cz> From: Tetsuo Handa Message-ID: <4ca702ae-9a67-fb4b-adcd-6668d43b7697@i-love.sakura.ne.jp> Date: Mon, 25 Jan 2021 23:20:41 +0900 User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.1 MIME-Version: 1.0 In-Reply-To: <20210125133244.GK827@dhcp22.suse.cz> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 2021/01/25 22:32, Michal Hocko wrote: > On Fri 22-01-21 19:47:42, Tetsuo Handa wrote: >> On 2021/01/22 10:35, Andrew Morton wrote: >>> On Wed, 20 Jan 2021 19:34:36 +0900 Tetsuo Handa wrote: >>> >>>> syzbot is reporting that memdup_user_nul() which receives user-controlled >>>> size (which can be up to (INT_MAX & PAGE_MASK)) via vfs_write() will hit >>>> order >= MAX_ORDER path [1]. > > That is nasty! That's because -EFAULT will not be detected before memory allocation succeeds. Fuzzer is passing huge size without corresponding valid buffer. syscall(__NR_write, r[0], 0x200000c0ul, 0x200000cbul); > >>>> Let's add __GFP_NOWARN to memdup_user_nul() as with commit 6c8fcc096be9d02f >>>> ("mm: don't let userspace spam allocations warnings"). Also use GFP_USER as >>>> with commit 6c2c97a24f096e32 ("memdup_user(): switch to GFP_USER"). > > No, this is papering over a more troubling underlying problem. Userspace > shouldn't be able to trigger an aribitrary higher order allocations. That requires inserting max size checking before calling memdup_user_nul(). Oh, scattering around such checking is not nice. Add max length argument into memdup_user_nul() like strndup_user() ? > Those users with a large size to copy should be really using kvmalloc > based (e.g vmemdup_user). No. The caller in this case (writing an entry to smackfs) is not expecting such large allocations. Sane allocation size would be always less than PAGE_SIZE. > >>> That commit failed to explain why a switch to GFP_USER was performed, >>> so that commit isn't a good substitute for an explanation of this >>> change. >> >> For example, commit 2f77d107050abc14 ("Fix incorrect user space access locking >> in mincore()") silently converted GFP_KERNEL to GFP_USER. >> >> #define GFP_KERNEL (__GFP_RECLAIM | __GFP_IO | __GFP_FS) >> #define GFP_USER (__GFP_RECLAIM | __GFP_IO | __GFP_FS | __GFP_HARDWALL) >> >> * %GFP_KERNEL is typical for kernel-internal allocations. The caller requires >> * %ZONE_NORMAL or a lower zone for direct access but can direct reclaim. >> >> * %GFP_USER is for userspace allocations that also need to be directly >> * accessibly by the kernel or hardware. It is typically used by hardware >> * for buffers that are mapped to userspace (e.g. graphics) that hardware >> * still must DMA to. cpuset limits are enforced for these allocations. >> >> * %__GFP_HARDWALL enforces the cpuset memory allocation policy. >> >>> >>> So... please fully describe the reason for this change right here in >>> this patch's changelog. >> >> I guess that GFP_USER is chosen by cautious developers when memory is >> allocated by userspace request. Is there a guideline for when to use GFP_USER ? > > I do not think we have anything better than the above. GFP_USER is > indeed used for userspace controlable allocations. So they can be a > subject to a more strict cpu policy. memdup_user_nul looks like a good > fit for GFP_USER to me. memdup_user and other variant already does this. > Hmm, Sabyrzhan already proposed a patch that adds size check to the caller, but it seems that that patch missed smk_write_ambient()/smk_write_onlycap()/smk_write_unconfined() etc. Oh, bug-prone approach. Why not handle at memdup_user_nul() side?