Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp326836ybl; Fri, 9 Aug 2019 06:38:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqyXsbKQITmxJb6Tz1h0KR0QgiapH2VXaJ3CyWZJOE+SDEvT83oQVB1wybcxPYI66014lrlr X-Received: by 2002:a63:3006:: with SMTP id w6mr17737640pgw.440.1565357898321; Fri, 09 Aug 2019 06:38:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565357898; cv=none; d=google.com; s=arc-20160816; b=trSW8SIYCb5JmM0gLL4SgQ6LqBufBBRqvJb78b/lf4Sp4IPHJPlaL0501gozIPyxU9 fymOil8REvoTmwx/WWYU76msF5rXGAos+G5imZlAQhBoWMAb+FIfCi7+djQHgaQATXO7 XQAJh0oNzNvuqoKXHS4z4KqtRmcTXsA6ca+Z/Sm7N2wCwMlv/T7Cwkv3QyqHsR9HIWaY SzuA2vro02HrZH307bvYpNSqS6qRZi67j6e5CQ5Eh70u9Q8xrUEgD1oyOltArLFXa8lU WONReT4W/64lbNNBIDCXoi/4V8Ik7MycaqMKwsa++OEX+HFpIGad6eU5UMPgIN6OrQ8C NdKQ== 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:openpgp:from:references:cc:to:subject:reply-to; bh=W/e3FaKnyiiHnPYNMIM+3ocFb3MozK25GrlOTfW37lE=; b=NTRD0v+iz1DwtUhNnaXzFeE24/jkIJ9qqb4UbW2oskPMSbMzUwL6hSNfDuZ8HKieeR yRkzXezOGn/1PURFROM2g65QPUspsLo3FApX28ZfBjWB/9o5g77mMTiiQOgBwUCSuZt4 m+MnkYOm91/QczgkDSl1uUIzXOX/scsyvgyJOmQKHfJPLn2nvADNZXTsPezE9e9QC6Mp 9e/lpwW1Fj9wuUyH3nFNAeTLRPAlhoF8jQkg67Sak9bHvwctRcCO9LjrtWBIZW7ArUW8 ZPQKrDpUnWEVzlFpT4qAbJfllXA01dKzcRJdBL3z30Jqtl45NuKJH/2Hl1q36Ntmv8/F BLwA== ARC-Authentication-Results: i=1; mx.google.com; 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 r3si4371939pjd.105.2019.08.09.06.38.01; Fri, 09 Aug 2019 06:38:18 -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; 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 S2436536AbfHINg7 (ORCPT + 99 others); Fri, 9 Aug 2019 09:36:59 -0400 Received: from mail-lf1-f65.google.com ([209.85.167.65]:43961 "EHLO mail-lf1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2406778AbfHINg5 (ORCPT ); Fri, 9 Aug 2019 09:36:57 -0400 Received: by mail-lf1-f65.google.com with SMTP id c19so69554953lfm.10; Fri, 09 Aug 2019 06:36:56 -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:openpgp :autocrypt:message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=W/e3FaKnyiiHnPYNMIM+3ocFb3MozK25GrlOTfW37lE=; b=cJUXzQ+EngxNSXGmZQeAzeS0lGTPIc/2xzp+oVRkZlx2693kcD9MghwO11xgZITeUC lQNwc6IHYMUrylvdj8cCsIXOHRlmZY3YO19eVZOER5v+lKBSzguMasyvm6PTl2VqJJPb fN7PGdCs1hu1wS8Fn5LgWKF7V5xrJ4o45PIYxZEqx7fI4LwTrjoRsiI9owYrQPd9CnVJ Gyu8WF6aEoqPFEBLXXKDuTS7MQz6qBwhoMmIWX2WJi/EbVK/EUSgaUAmriqAYbFkalg8 V1yTNLnPU9Kx/4PVd23I0b5R0rCLKS8hMLPcksRvh3n+76xVAbgyS8ZSjfWN53Ghi0fL dxLQ== X-Gm-Message-State: APjAAAWzgJN+1cfGphhbqKU4GoAQBiXU4UpGnscTLiO0qRbjmZzPUjry BkaW6B05S41ALf+sICNOiNc= X-Received: by 2002:a19:a419:: with SMTP id q25mr13024216lfc.136.1565357815476; Fri, 09 Aug 2019 06:36:55 -0700 (PDT) Received: from [192.168.42.52] ([213.87.147.2]) by smtp.gmail.com with ESMTPSA id a70sm19395997ljf.57.2019.08.09.06.36.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 09 Aug 2019 06:36:54 -0700 (PDT) Reply-To: alex.popov@linux.com Subject: Re: [PATCH] floppy: fix usercopy direction To: Jann Horn , Jens Axboe Cc: Jiri Kosina , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Al Viro , Mukesh Ojha , "kernel-hardening@lists.openwall.com" , Denis Efremov , Julia Lawall , Gilles Muller , Nicolas Palix , Michal Marek , cocci@systeme.lip6.fr References: <20190326220348.61172-1-jannh@google.com> From: Alexander Popov Openpgp: preference=signencrypt Autocrypt: addr=alex.popov@linux.com; prefer-encrypt=mutual; keydata= mQINBFX15q4BEADZartsIW3sQ9R+9TOuCFRIW+RDCoBWNHhqDLu+Tzf2mZevVSF0D5AMJW4f UB1QigxOuGIeSngfmgLspdYe2Kl8+P8qyfrnBcS4hLFyLGjaP7UVGtpUl7CUxz2Hct3yhsPz ID/rnCSd0Q+3thrJTq44b2kIKqM1swt/F2Er5Bl0B4o5WKx4J9k6Dz7bAMjKD8pHZJnScoP4 dzKPhrytN/iWM01eRZRc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N8tK3CCd +aKPCxUFfil62DfTa7YgLovr7sHQB+VMQkNDPXleC+amNqJb423L8M2sfCi9gw/lA1ha6q80 ydgbcFEkNjqz4OtbrSwEHMy/ADsUWksYuzVbw7/pQTc6OAskESBr5igP7B/rIACUgiIjdOVB ktD1IQcezrDcuzVCIpuq8zC6LwLm7V1Tr6zfU9FWwnqzoQeQZH4QlP7MBuOeswCpxIl07mz9 jXz/74kjFsyRgZA+d6a1pGtOwITEBxtxxg== Message-ID: <9ced7a06-5048-ad1a-3428-c8f943f7469c@linux.com> Date: Fri, 9 Aug 2019 16:36:52 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <20190326220348.61172-1-jannh@google.com> 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 Hello everyone! On 27.03.2019 1:03, Jann Horn wrote: > As sparse points out, these two copy_from_user() should actually be > copy_to_user(). I've spent some time on these bugs, but it turned out that they are already public. I think Jann's patch is lost, it is not applied to the mainline. So I add a new floppy maintainer Denis Efremov to CC. These bugs on x86_64 cause memset for the userspace memory from the kernelspace. That is funny: - access_ok for the copy_from_user source (2nd argument) returns zero; - copy_from_user tries to erase the destination (1st argument); - but the destination is in the userspace instead of kernelspace. So we have: [ 40.937098] BUG: unable to handle page fault for address: 0000000041414242 [ 40.938714] #PF: supervisor write access in kernel mode [ 40.939951] #PF: error_code(0x0002) - not-present page [ 40.941121] PGD 7963f067 P4D 7963f067 PUD 0 [ 40.942107] Oops: 0002 [#1] SMP NOPTI [ 40.942968] CPU: 0 PID: 292 Comm: d Not tainted 5.3.0-rc3+ #7 [ 40.944288] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Ubuntu-1.8.2-1ubuntu1 04/01/2014 [ 40.946478] RIP: 0010:__memset+0x24/0x30 [ 40.947394] Code: 90 90 90 90 90 90 0f 1f 44 00 00 49 89 f9 48 89 d1 83 e2 07 48 c1 e9 03 40 0f b6 f6 48 b8 01 01 01 01 01 01 01 01 48 0f af c6 48 ab 89 d1 f3 aa 4c 89 c8 c3 90 49 89 f9 40 88 f0 48 89 d1 f3 [ 40.951721] RSP: 0018:ffffc900003dbd58 EFLAGS: 00010206 [ 40.952941] RAX: 0000000000000000 RBX: 0000000000000034 RCX: 0000000000000006 [ 40.954592] RDX: 0000000000000004 RSI: 0000000000000000 RDI: 0000000041414242 [ 40.956169] RBP: 0000000041414242 R08: ffffffff8200bd80 R09: 0000000041414242 [ 40.957753] R10: 0000000000121806 R11: ffff88807da28ab0 R12: ffffc900003dbd7c [ 40.959407] R13: 0000000000000001 R14: 0000000041414242 R15: 0000000041414242 [ 40.961062] FS: 00007f91115c4440(0000) GS:ffff88807da00000(0000) knlGS:0000000000000000 [ 40.962603] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 40.963695] CR2: 0000000041414242 CR3: 000000007c584000 CR4: 00000000000006f0 [ 40.965004] Call Trace: [ 40.965459] _copy_from_user+0x51/0x60 [ 40.966141] compat_getdrvstat+0x124/0x170 [ 40.966781] fd_compat_ioctl+0x69c/0x6d0 [ 40.967423] ? selinux_file_ioctl+0x16f/0x210 [ 40.968117] compat_blkdev_ioctl+0x21d/0x8f0 [ 40.968864] __x32_compat_sys_ioctl+0x99/0x250 [ 40.969659] do_syscall_64+0x4a/0x110 [ 40.970337] entry_SYSCALL_64_after_hwframe+0x44/0xa9 I haven't found a way to exploit it. > Fixes: 229b53c9bf4e ("take floppy compat ioctls to sodding floppy.c") > Cc: stable@vger.kernel.org > Signed-off-by: Jann Horn > Reviewed-by: Mukesh Ojha > --- > compile-tested only Acked-by: Alexander Popov > drivers/block/floppy.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c > index 95f608d1a098..8c641245ff12 100644 > --- a/drivers/block/floppy.c > +++ b/drivers/block/floppy.c > @@ -3749,7 +3749,7 @@ static int compat_getdrvprm(int drive, > v.native_format = UDP->native_format; > mutex_unlock(&floppy_mutex); > > - if (copy_from_user(arg, &v, sizeof(struct compat_floppy_drive_params))) > + if (copy_to_user(arg, &v, sizeof(struct compat_floppy_drive_params))) > return -EFAULT; > return 0; > } > @@ -3785,7 +3785,7 @@ static int compat_getdrvstat(int drive, bool poll, > v.bufblocks = UDRS->bufblocks; > mutex_unlock(&floppy_mutex); > > - if (copy_from_user(arg, &v, sizeof(struct compat_floppy_drive_struct))) > + if (copy_to_user(arg, &v, sizeof(struct compat_floppy_drive_struct))) > return -EFAULT; > return 0; > Eintr: > I also wrote a coccinelle rule for detecting similar bugs (adding coccinelle experts to CC). virtual report @cfu@ identifier f; type t; identifier v; position decl_p; position copy_p; @@ f(..., t v@decl_p, ...) { <+... copy_from_user@copy_p(v, ...) ...+> } @script:python@ f << cfu.f; t << cfu.t; v << cfu.v; decl_p << cfu.decl_p; copy_p << cfu.copy_p; @@ if '__user' in t: msg0 = "function \"" + f + "\" has arg \"" + v + "\" of type \"" + t + "\"" coccilib.report.print_report(decl_p[0], msg0) msg1 = "copy_from_user uses \"" + v + "\" as the destination. What a shame!\n" coccilib.report.print_report(copy_p[0], msg1) The rule output: ./drivers/block/floppy.c:3756:49-52: function "compat_getdrvprm" has arg "arg" of type "struct compat_floppy_drive_params __user *" ./drivers/block/floppy.c:3783:5-19: copy_from_user uses "arg" as the destination. What a shame! ./drivers/block/floppy.c:3789:49-52: function "compat_getdrvstat" has arg "arg" of type "struct compat_floppy_drive_struct __user *" ./drivers/block/floppy.c:3819:5-19: copy_from_user uses "arg" as the destination. What a shame! Best regards, Alexander