Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2724673ybl; Sun, 2 Feb 2020 05:19:15 -0800 (PST) X-Google-Smtp-Source: APXvYqxlWDkDpjqM7Ec0P+kXNSv7JVV0UK6mGGZZkUfVm2vcfc5cWWJNQ6Mw8x2cDK8R2gPj5Uvm X-Received: by 2002:a05:6830:1294:: with SMTP id z20mr14068089otp.60.1580649555794; Sun, 02 Feb 2020 05:19:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580649555; cv=none; d=google.com; s=arc-20160816; b=Skc7iegeXJmbEgRUdiZkax4VJSOSwoT6HmuxNoTY0pqWnzkcNiEjxeL/X2gYd8mvR6 z64CFL6h1a38gzm4hmqB2BQDM64I7bL9lMGLc4G4t5nXNfLsqVo8x7dajgzZVPZPgzuA ef5YO//GJaHqHjA2hcgshJhodhtxNh6B9TJfz6TJEpPvVRFPii+LWGMBp/URDUyVL/Bx IPpoh6oenWYCkM5ytmZHzIzNDTNcayFv7ANEkgHXUy28IxwnamDpmUfXSbism8Ux5htx 6T6+X2geez9pXfMkIsbbSHXVVdvdZ1AajRFiYy3iauz5TgPkUeOxTMN8t8dYUylikgv8 3TGw== 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; bh=+JexwpZ7uOUAzO/bZPt69XOsxgXW5ev8PRFd02TcEk8=; b=lac1zgQxZUhfAgFEBDmmRF5Av5Czbb2zPQNY32aDVvyduXYUgoCBddMqF/o2VHEBJr 69UGOQbbkcaS2L8BcBpyh53TqNcg9J/PZw63nOncnkwv5/Vs+XEMjLtONIzY5MHUsPRe /MAeiU4Ezs9TmREc/2Ay9zE+Ts5e89aCrvdopgSskjLrw79YZSjGeueCe7mpU+naExT1 jBkkvsFBXggr5b/x39u/aKiaUlk44hkvOLyRzQmqrS3col81xAW3fd7O7pTp0HzeU/Y3 syAuU3ffC1tjDwwBaxDlfyRrkz7+CIDf0ZMD8zxfqE20gW1srDDS29FpwgcrjduVhLK6 8zKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=IE+nMVkX; 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 d22si7554661oti.316.2020.02.02.05.18.51; Sun, 02 Feb 2020 05:19:15 -0800 (PST) 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=@ffwll.ch header.s=google header.b=IE+nMVkX; 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 S1726877AbgBBNRO (ORCPT + 99 others); Sun, 2 Feb 2020 08:17:14 -0500 Received: from mail-ot1-f65.google.com ([209.85.210.65]:37142 "EHLO mail-ot1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726669AbgBBNRO (ORCPT ); Sun, 2 Feb 2020 08:17:14 -0500 Received: by mail-ot1-f65.google.com with SMTP id d3so11081099otp.4 for ; Sun, 02 Feb 2020 05:17:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=+JexwpZ7uOUAzO/bZPt69XOsxgXW5ev8PRFd02TcEk8=; b=IE+nMVkXga9fWUwZKPOjqNIk95sq5yO14vZeu1ludYC7s0JaU1fr2jelppwPGlACgG 83h28kTdOijU1EWLsvch7JEqnA+Ea0m9DfYb62/uQy108/7UV+m83daOiNSPwAckbp8F DpBcwguqXp7EEyyRRPBYZizpZiRhl1W8LshjY= 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=+JexwpZ7uOUAzO/bZPt69XOsxgXW5ev8PRFd02TcEk8=; b=VieXIfIO+rZbppqPClqBT2+a9aYit2ghG2pVKIfTwl+N8ovyicmoYnQftDaSjFRuIR cJLEAffIzqHd3ico9tW+aAdYuiEUdZY1XM76WGop6GCyBy/5fUVq9sf1lc/mA4pCtc4f EaK+cBU/ThLMl7S1azg3h24jM6e9gPsdRkSi7XJIrX2L/9DnXtB7+VBE253N3cIxdSYW zq1dTIasn9E/QZ0WIwAVrUdQRWFRZap8PgNtcYspTC7fW/ZC3GpnwXqEbwj3ZBw+fIGG ljKECZWxLRMIuBFBLvaGWrad0xVrApTBMLuvenhO8a7IjngxG7LC2QRogfwFUEYeB13d XcmQ== X-Gm-Message-State: APjAAAWYudjEd4kxMVN8ZJBA2xsnECQDrBgjXWuUJWruttTsOuCN8krr yH30ZQdssm40kX5s6xkwD/giB7uClfUeNHo5a5WH3g== X-Received: by 2002:a9d:7696:: with SMTP id j22mr14988663otl.188.1580649432010; Sun, 02 Feb 2020 05:17:12 -0800 (PST) MIME-Version: 1.0 References: <20200201043209.13412-1-hdanton@sina.com> <20200201090247.10928-1-hdanton@sina.com> <20200201162537.GK1778@kadam> In-Reply-To: <20200201162537.GK1778@kadam> From: Daniel Vetter Date: Sun, 2 Feb 2020 14:17:00 +0100 Message-ID: Subject: Re: KASAN: use-after-free Read in vgem_gem_dumb_create To: Dan Carpenter Cc: Hillf Danton , syzbot , Dave Airlie , Alex Deucher , amd-gfx list , "Wilson, Chris" , =?UTF-8?Q?Christian_K=C3=B6nig?= , David Miller , dri-devel , Emil Velikov , "Anholt, Eric" , "moderated list:DMA BUFFER SHARING FRAMEWORK" , Linux Kernel Mailing List , "open list:DMA BUFFER SHARING FRAMEWORK" , netdev , Rob Clark , Sean Paul , Sumit Semwal , syzkaller-bugs 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 Sat, Feb 1, 2020 at 5:26 PM Dan Carpenter wrote: > > On Sat, Feb 01, 2020 at 05:02:47PM +0800, Hillf Danton wrote: > > > > On Sat, 1 Feb 2020 09:17:57 +0300 Dan Carpenter wrote: > > > On Sat, Feb 01, 2020 at 12:32:09PM +0800, Hillf Danton wrote: > > > > > > > > Release obj in error path. > > > > > > > > --- a/drivers/gpu/drm/vgem/vgem_drv.c > > > > +++ b/drivers/gpu/drm/vgem/vgem_drv.c > > > > @@ -196,10 +196,10 @@ static struct drm_gem_object *vgem_gem_c > > > > return ERR_CAST(obj); > > > > > > > > ret = drm_gem_handle_create(file, &obj->base, handle); > > > > - drm_gem_object_put_unlocked(&obj->base); > > > > - if (ret) > > > > + if (ret) { > > > > + drm_gem_object_put_unlocked(&obj->base); > > > > return ERR_PTR(ret); > > > > - > > > > + } > > > > return &obj->base; > > > > > > Oh yeah. It's weird that we never noticed the success path was broken. > > > It's been that way for three years and no one noticed at all. Very > > > strange. > > > > > > Anyway, it already gets freed on error in drm_gem_handle_create() so > > > we should just delete the drm_gem_object_put_unlocked() here it looks > > > like. There's two refcounts here, one is the handle_count, and the other is the underlying object refcount. I think the code is correct, except if you race with a 2nd thread which destroys the object (through the handle) while we still try to read gem_object->size in the caller of this. So correct fix (I think at least) is to shuffle that temporary reference on the object (not the handle) we hold while constructing it around a bit, so there's no use-after free anymore in the case of a race. I'm typing a patch for this. Cheers, Daniel > > Good catch, Dan :P > > Would you please post a patch sometime convenient next week? > > Sure. Will do. > > regards, > dan carpenter > -- Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch