Received: by 10.213.65.68 with SMTP id h4csp1160906imn; Wed, 4 Apr 2018 13:46:27 -0700 (PDT) X-Google-Smtp-Source: AIpwx48VPaB5pd2XIlCulug4InzdlwOhtgVbGJ/W1dnnpWs1bvLt4QFpEFgw5HY5/fbl09DYJMts X-Received: by 2002:a17:902:ab98:: with SMTP id f24-v6mr19718699plr.331.1522874787681; Wed, 04 Apr 2018 13:46:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522874787; cv=none; d=google.com; s=arc-20160816; b=ZzB1VMwRcvCo1ydNxmVI4JJdjyDzzE1IgRj03Z2dHQqdYNJw1R90sSagqyl/kFP4Zz aDe8cmJnwQBN2YZr232F+lvM6rWlP63TVsdKVFiVFszO4kV/DhJ+H86BodyH/yZsskCt HZ7esCkNN8YQBZEjrKK00BRqqiCArP2cZDlBwlsPq/WQsCNuVKC+RuekHzXCDh8oUtz8 ncVX5IcyiyH0CqtODqozw2u+ULNkuQMAFfZoYrLyR/ORSE55znydzOzalQHo7WjZTL1X wn4/g69H588oQPYClKiN98bGzoKwibvDxqxyxcO3qnMk+6PqaQNP97FROAnyLex5XuF7 mzuA== 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:from:references:cc:to:subject:reply-to :arc-authentication-results; bh=fRMAX/9532N5n3ksxQDPoHkUEPawlYGkkvEXZGz94as=; b=n0kOWh0O+2FKBPwjeGzAXI7DhQtf+iZ3rTdk8Kj9EoWDvvNPMzeg1qbAf3RZBbIFve PmS/1VFqWgO+eVysUwUnHkMQDM+NNW2tlVi2dJCaxLhEV82YVgJ8sktGl0IApynoGOv7 8ALFup3/iqxlRXJl44ql6RH+jUGJGSErCWqAk1td1ZvOUcM3mqvyKXv2GldwDZz+HAp1 Mp6jps/fSI2xgyAswlPTAmwCH+ZhbnP6k7Nd3GeUB0JiBzNDFtNOZhkgihY5T9E8mqNa 35vsRXXRxoQD/JrHKIN9JeuIQSNEeUxueXKUflc9s8S1jzzuVwIgXfMgzRQEcVJKtPVp LU7A== 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 v2-v6si3988422plo.670.2018.04.04.13.46.12; Wed, 04 Apr 2018 13:46:27 -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 S1752077AbeDDUpD (ORCPT + 99 others); Wed, 4 Apr 2018 16:45:03 -0400 Received: from smtp.infotech.no ([82.134.31.41]:35704 "EHLO smtp.infotech.no" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751754AbeDDUpB (ORCPT ); Wed, 4 Apr 2018 16:45:01 -0400 Received: from localhost (localhost [127.0.0.1]) by smtp.infotech.no (Postfix) with ESMTP id B724820417C; Wed, 4 Apr 2018 22:44:59 +0200 (CEST) X-Virus-Scanned: by amavisd-new-2.6.6 (20110518) (Debian) at infotech.no Received: from smtp.infotech.no ([127.0.0.1]) by localhost (smtp.infotech.no [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id jJJ13WeHbydm; Wed, 4 Apr 2018 22:44:53 +0200 (CEST) Received: from [192.168.48.23] (host-184-164-15-239.dyn.295.ca [184.164.15.239]) by smtp.infotech.no (Postfix) with ESMTPA id 9E791204179; Wed, 4 Apr 2018 22:44:52 +0200 (CEST) Reply-To: dgilbert@interlog.com Subject: Re: usercopy whitelist woe in scsi_sense_cache To: Kees Cook , Oleksandr Natalenko Cc: David Windsor , "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org, LKML References: <10360653.ov98egbaqx@natalenko.name> From: Douglas Gilbert Message-ID: Date: Wed, 4 Apr 2018 16:44:51 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-CA Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2018-04-04 04:21 PM, Kees Cook wrote: > On Wed, Apr 4, 2018 at 12:07 PM, Oleksandr Natalenko > wrote: >> With v4.16 I get the following dump while using smartctl: >> [...] >> [ 261.262135] Bad or missing usercopy whitelist? Kernel memory exposure >> attempt detected from SLUB object 'scsi_sense_cache' (offset 94, size 22)! >> [...] >> [ 261.345976] Call Trace: >> [ 261.350620] __check_object_size+0x130/0x1a0 >> [ 261.355775] sg_io+0x269/0x3f0 >> [ 261.360729] ? path_lookupat+0xaa/0x1f0 >> [ 261.364027] ? current_time+0x18/0x70 >> [ 261.366684] scsi_cmd_ioctl+0x257/0x410 >> [ 261.369871] ? xfs_bmapi_read+0x1c3/0x340 [xfs] >> [ 261.372231] sd_ioctl+0xbf/0x1a0 [sd_mod] >> [ 261.375456] blkdev_ioctl+0x8ca/0x990 >> [ 261.381156] ? read_null+0x10/0x10 >> [ 261.384984] block_ioctl+0x39/0x40 >> [ 261.388739] do_vfs_ioctl+0xa4/0x630 >> [ 261.392624] ? vfs_write+0x164/0x1a0 >> [ 261.396658] SyS_ioctl+0x74/0x80 >> [ 261.399563] do_syscall_64+0x74/0x190 >> [ 261.402685] entry_SYSCALL_64_after_hwframe+0x3d/0xa2 > > This is: > > sg_io+0x269/0x3f0: > blk_complete_sghdr_rq at block/scsi_ioctl.c:280 > (inlined by) sg_io at block/scsi_ioctl.c:376 > > which is: > > if (req->sense_len && hdr->sbp) { > int len = min((unsigned int) hdr->mx_sb_len, req->sense_len); > > if (!copy_to_user(hdr->sbp, req->sense, len)) > hdr->sb_len_wr = len; > else > ret = -EFAULT; > } > >> [...] >> I can easily reproduce it with a qemu VM and 2 virtual SCSI disks by calling >> smartctl in a loop and doing some usual background I/O. The warning is >> triggered within 3 minutes or so (not instantly). >> >> Initially, it was produced on my server after a kernel update (because disks >> are monitored with smartctl via Zabbix). >> >> Looks like the thing was introduced with >> 0afe76e88c57d91ef5697720aed380a339e3df70. >> >> Any idea how to deal with this please? If needed, I can provide any additional >> info, and also I'm happy/ready to test any proposed patches. > > Interesting, and a little confusing. So, what's strange here is that > the scsi_sense_cache already has a full whitelist: > > kmem_cache_create_usercopy("scsi_sense_cache", > SCSI_SENSE_BUFFERSIZE, 0, SLAB_HWCACHE_ALIGN, > 0, SCSI_SENSE_BUFFERSIZE, NULL); > > Arg 2 is the buffer size, arg 5 is the whitelist offset (0), and the > whitelist size (same as arg2). In other words, the entire buffer > should be whitelisted. > > include/scsi/scsi_cmnd.h says: > > #define SCSI_SENSE_BUFFERSIZE 96 > > That means scsi_sense_cache should be 96 bytes in size? But a 22 byte > read starting at offset 94 happened? That seems like a 20 byte read > beyond the end of the SLUB object? Though if it were reading past the > actual end of the object, I'd expect the hardened usercopy BUG (rather > than the WARN) to kick in. Ah, it looks like > /sys/kernel/slab/scsi_sense_cache/slab_size shows this to be 128 bytes > of actual allocation, so the 20 bytes doesn't strictly overlap another > object (hence no BUG): > > /sys/kernel/slab/scsi_sense_cache# grep . object_size usersize slab_size > object_size:96 > usersize:96 > slab_size:128 > > Ah, right, due to SLAB_HWCACHE_ALIGN, the allocation is rounded up to > the next cache line size, so there's 32 bytes of padding to reach 128. > > James or Martin, is this over-read "expected" behavior? i.e. does the > sense cache buffer usage ever pull the ugly trick of silently > expanding its allocation into the space the slab allocator has given > it? If not, this looks like a real bug. > > What I don't see is how req->sense is _not_ at offset 0 in the > scsi_sense_cache object... Looking at the smartctl SCSI code it pulls 32 byte sense buffers. Can't see 22 anywhere relevant in its code. There are two types of sense: fixed and descriptor: with fixed you seldom need more than 18 bytes (but it can only represent 32 bit LBAs). The other type has a header and 0 or more variable length descriptors. If decoding of descriptor sense went wrong you might end up at offset 94. But not with smartctl .... Doug Gilbert