Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp1120567ybg; Wed, 29 Jul 2020 06:25:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwzeDJZHftLYS5bPI2BPouudjP65G8pUUSxfsj5HutdJ9TVqFW8IUxf1vn3BtQaPWYDtlDE X-Received: by 2002:a17:906:c8d2:: with SMTP id gc18mr12930930ejb.24.1596029105976; Wed, 29 Jul 2020 06:25:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596029105; cv=none; d=google.com; s=arc-20160816; b=Jhksqf06OBejl0FfC4d4L44+KKFpLPIO7EUbN4BR8oohKGnfEBlhyVC46hvfymEjO+ lkPNhIpZkWiox7XV+SSG7st/bmBc3I+HGGWgsy3KqmyV2gQoV7znRiaeTElO9bTJRem0 AGI9SJsD5f+oiWOgRfG+DPfcaN2YSw2cpM/b4Unq72TiGlVFQiS9y+hcbE3bR92e7wRp ZJph14qlE2EdvSjz4+veXu6iqCPZOUXj/pMJlaqSdiwfOuZqZODfewpfJFTlGE1bOXRN 00emSaLOldc522P3PdOmmaE9fLoRjab6AeRoL6mVh9YzaLypJx3YxkrrdqkMDRYt8rdJ iTgQ== 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:autocrypt:from:references:cc:to:subject:reply-to; bh=cZW4UMP8peVI8CAVoM7H793wrysqn0IQcJWFXMg3ih4=; b=Gq8xqElI6m4kNgBlSCKAOUaIdF+zjh74++O1CBnuCFM0L6wCk8FnTZ9QI0smb75nsM HHRwA6m2AX16+vEkm0SK6twGuoMnLndB3Dz7phoneFMjfmV/zaUs/H1f/tZBvCmKpkmL 1yQV2QGVzer6DOFPoFKJuMTa5x+Ao9I2o+k5p6dPyr5VefsRxa58GMzUx2T56ED3LQ/a 7uIdVM8lsYVMqpR8SZGV74rTQZbOLP9KIcXTg7Ag9C6IuTs3kyzlSleGCZDrLcB0Ja61 DFodu2wtEsoElrESbQ0gzerWrpzEpzhxOAKGmNiYIf5StjuyliX0SxQg70XEtIMLnkrD 48NA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ot3si981523ejb.655.2020.07.29.06.24.43; Wed, 29 Jul 2020 06:25:05 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727087AbgG2NWs (ORCPT + 99 others); Wed, 29 Jul 2020 09:22:48 -0400 Received: from mail-lf1-f67.google.com ([209.85.167.67]:41328 "EHLO mail-lf1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726496AbgG2NWr (ORCPT ); Wed, 29 Jul 2020 09:22:47 -0400 Received: by mail-lf1-f67.google.com with SMTP id i19so12998861lfj.8; Wed, 29 Jul 2020 06:22:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:subject:to:cc:references:from:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=cZW4UMP8peVI8CAVoM7H793wrysqn0IQcJWFXMg3ih4=; b=stuRB39aFs9gQ10nOF8txLiJK0tbrA/31IBZcouxXqoFNHbtaM6LGN34h1KhUvbwZH UDsTdhpr0u1w1JvzIdkIEwle/G3uovZLPJ1G0fSeBmezR9m3AylqRiQF8JZxOsd11K2h OhYaPhHT21vY7QKkEXxCPrvNqJBEWSwOs3Gqx+ONND2PPhm5YphfJnBHBFJ2W2A4zfnU HoUhHUT23itDMbaTikRQs+/UaM9xPS4Ud3hX/JcUvhvlmkTxq2SfrbFVKcfjRqUMKX7f nySFUPTUw1g4WyN6wcAkohmhzrQJlVTDWssujyPJWpKlA2eWsWRqVaxovzvKBD+NPTGa tI1A== X-Gm-Message-State: AOAM531K8eSMnjl3f18anUtgWYMufCK8+eBgloqOn6XxKh8/E8TEQLCv xokJokaZhdTECJf4raiVmTM= X-Received: by 2002:ac2:5223:: with SMTP id i3mr17629828lfl.57.1596028963727; Wed, 29 Jul 2020 06:22:43 -0700 (PDT) Received: from [10.68.32.147] (broadband-37-110-38-130.ip.moscow.rt.ru. [37.110.38.130]) by smtp.gmail.com with ESMTPSA id y1sm555490lfb.45.2020.07.29.06.22.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Jul 2020 06:22:42 -0700 (PDT) Reply-To: efremov@linux.com Subject: Re: [Linux-kernel-mentees] [PATCH v2] block/floppy: Prevent kernel-infoleak in raw_cmd_copyout() To: Dan Carpenter , Peilin Ye Cc: Jens Axboe , Arnd Bergmann , linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel-mentees@lists.linuxfoundation.org References: <20200728141946.426245-1-yepeilin.cs@gmail.com> <20200729115157.8519-1-yepeilin.cs@gmail.com> <20200729125820.GB1840@kadam> From: Denis Efremov Autocrypt: addr=efremov@linux.com; keydata= mQINBFsJUXwBEADDnzbOGE/X5ZdHqpK/kNmR7AY39b/rR+2Wm/VbQHV+jpGk8ZL07iOWnVe1 ZInSp3Ze+scB4ZK+y48z0YDvKUU3L85Nb31UASB2bgWIV+8tmW4kV8a2PosqIc4wp4/Qa2A/ Ip6q+bWurxOOjyJkfzt51p6Th4FTUsuoxINKRMjHrs/0y5oEc7Wt/1qk2ljmnSocg3fMxo8+ y6IxmXt5tYvt+FfBqx/1XwXuOSd0WOku+/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I15NZ/Xy9ADAL0TpEg3fNVad2eslhCwiQViWfKOGOLLMe7v zod9dwxYdGXnNRlW+YOCdFNVPMvPDr08zgzXaZ2+QJjp44HSyzxgONmHAroFcqCFUlfAqUDO T30gV5bQ8BHqvfWyEhJT+CS3JJyP8BmmSgPa0Adlp6Do+pRsOO1YNNO78SYABhMi3fEa7X37 WxL31TrNCPnIauTgZtf/KCFQJpKaakC3ffEkPhyTjEl7oOE9xccNjccZraadi+2uHV0ULA1m ycHhb817A03n1I00QwLf2wOkckdqTqRbFFI/ik69hF9hemK/BmAHpShI+z1JsYT9cSs8D7wb aF/jQVy4URensgAPkgXsRiboqOj/rTz9F5mpd/gPU/IOUPFEMoo4TInt/+dEVECHioU3RRrW EahrGMfRngbdp/mKs9aBR56ECMfFFUPyI3VJsNbgpcIJjV/0N+JdJKQpJ/4uQ2zNm0wH/RU8 CRJvEwtKemX6fp/zLI36Gvz8zJIjSBIEqCb7vdgvWarksrhmi6/Jay5zRZ03+k6YwiqgX8t7 ANwvYa1h1dQ36OiTqm1cIxRCGl4wrypOVGx3OjCar7sBLD+NkwO4RaqFvdv0xuuy4x01VnOF Message-ID: Date: Wed, 29 Jul 2020 16:22:41 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20200729125820.GB1840@kadam> 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 7/29/20 3:58 PM, Dan Carpenter wrote: > Argh... This isn't right still. The "ptr" comes from raw_cmd_copyin() > > ptr = kmalloc(sizeof(struct floppy_raw_cmd), GFP_KERNEL); > copy_from_user overwrites the padding bytes: ptr = kmalloc(sizeof(struct floppy_raw_cmd), GFP_KERNEL); if (!ptr) return -ENOMEM; *rcmd = ptr; ret = copy_from_user(ptr, param, sizeof(*ptr)); I think memcpy should be safe in this patch. I've decided to dig a bit into the issue and to run some tests. Here are my observations: $ cat test.c #include #include #define __user struct floppy_raw_cmd { unsigned int flags; void __user *data; char *kernel_data; /* location of data buffer in the kernel */ struct floppy_raw_cmd *next; /* used for chaining of raw cmd's * within the kernel */ long length; /* in: length of dma transfer. out: remaining bytes */ long phys_length; /* physical length, if different from dma length */ int buffer_length; /* length of allocated buffer */ unsigned char rate; #define FD_RAW_CMD_SIZE 16 #define FD_RAW_REPLY_SIZE 16 #define FD_RAW_CMD_FULLSIZE (FD_RAW_CMD_SIZE + 1 + FD_RAW_REPLY_SIZE) unsigned char cmd_count; union { struct { unsigned char cmd[FD_RAW_CMD_SIZE]; unsigned char reply_count; unsigned char reply[FD_RAW_REPLY_SIZE]; }; unsigned char fullcmd[FD_RAW_CMD_FULLSIZE]; }; int track; int resultcode; int reserved1; int reserved2; }; void __attribute__((noinline)) stack_alloc() { struct floppy_raw_cmd stack; memset(&stack, 0xff, sizeof(struct floppy_raw_cmd)); asm volatile ("" ::: "memory"); } int __attribute__((noinline)) test(struct floppy_raw_cmd *ptr) { struct floppy_raw_cmd cmd = *ptr; int i; for (i = 0; i < sizeof(struct floppy_raw_cmd); ++i) { if (((char *)&cmd)[i]) { printf("leak[%d]\n", i); return i; } } return 0; } int main(int argc, char *argv[]) { struct floppy_raw_cmd zero; memset(&zero, 0, sizeof(struct floppy_raw_cmd)); // For selfcheck uncomment: // zero.resultcode = 1; stack_alloc(); return test(&zero); } Next, I've prepared containers with gcc 4.8 5 6 7 8 9 10 versions with this tool (https://github.com/a13xp0p0v/kernel-build-containers). And checked for leaks on x86_64 with the script test.sh $ cat test.sh #!/bin/bash for i in 4.8 5 6 7 8 9 10 do ./run_container.sh gcc-$i $(pwd)/src $(pwd)/out bash -c 'gcc test.c; ./a.out' ./run_container.sh gcc-$i $(pwd)/src $(pwd)/out bash -c 'gcc -O2 test.c; ./a.out' ./run_container.sh gcc-$i $(pwd)/src $(pwd)/out bash -c 'gcc -O3 test.c; ./a.out' done No leaks reported. Is it really possible this this kind of init, i.e. cmd = *ptr? https://lwn.net/Articles/417989/ (December 1, 2010). GCC 4.9.4 released [2016-08-03] Maybe this behavior changed. https://www.nccgroup.com/us/about-us/newsroom-and-events/blog/2019/october/padding-the-struct-how-a-compiler-optimization-can-disclose-stack-memory/ Reports for >= 4.7, < 8.0 version. But I can't find a word about this kind of inits: cmd = *ptr. Thanks, Denis