Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp891169pxj; Sat, 8 May 2021 01:36:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxnMf/eccKSqM9nOEXjMdOjGqSfvaA64RztXmtfLhiiZMxztI5cLBK+bZy2KCzXiOnc03cr X-Received: by 2002:a17:907:2176:: with SMTP id rl22mr14703952ejb.155.1620463004152; Sat, 08 May 2021 01:36:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620463004; cv=none; d=google.com; s=arc-20160816; b=AmblNHju3JHHVWMZfgH08VuBqBGlqdISF2FBbOznf/dl8poo1MEhND2WnXTfhgQ65P KFPhCFLXDQkzrFTv1WZBoyPD7fT16epBHjcE6f+JiEQmiM7ts2sYmjHmC6Ezm/2+ANg9 jnnynEqanzqWg//QNaQ0o4kiIMLN42mqao/o1iZESZNPO8pH4AKX2kZ6ylEtjBuoMcTL itR3tjuV5p3JMNkb/hqqrXRjfP8K5fCUzEjfCm5CI2AadxWxlLoOggo9qvhSk8l1MQHv jCiT3IupS61hPRnvoNfU4lnqgwLuAobN2JFBHQRVDnkobuodn9zeGhgr4aLr2dH1Nm2L oriA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=GQvUcd2bMYxPOFUAKWZ4rFYcQi8hK52tVHiFNiIyW3s=; b=B35Cv2ghnFDDcbw2xiwDblm4YH3xJOqBpLhS2yp8uqdkWo7TTs1Yhljrl9uEU1iafo aVZfXFyNLIpv9YTGDE281gNGIOJq/B8Z3f9Cj9h6BzpXoK+JwPzWrxbBAfYQYGrZeGug DrO3ZvWBJNjv8V4wqazeMDUxNV0s8NSui3iR/onrwfOMDKRf1DR9eKrpDGV57MtEYg18 GE1Wt/MlRp3MPyqysDUcgeZIqI2rL/4CPHsyzqatees0Xjabtemm5kbePERSW+ACxzqS 6inTOEozQq3SQNmntEK/jeuHSpuz39opn3hEX3KK5AGuay7xabWw0QKsVJIomC43oEW+ mC4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=gOZkzac6; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r14si4291576edy.200.2021.05.08.01.36.19; Sat, 08 May 2021 01:36:44 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=gOZkzac6; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229947AbhEHIfo (ORCPT + 99 others); Sat, 8 May 2021 04:35:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229583AbhEHIfn (ORCPT ); Sat, 8 May 2021 04:35:43 -0400 Received: from mail-oi1-x22a.google.com (mail-oi1-x22a.google.com [IPv6:2607:f8b0:4864:20::22a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 561F5C061574 for ; Sat, 8 May 2021 01:34:41 -0700 (PDT) Received: by mail-oi1-x22a.google.com with SMTP id m13so10985070oiw.13 for ; Sat, 08 May 2021 01:34:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=GQvUcd2bMYxPOFUAKWZ4rFYcQi8hK52tVHiFNiIyW3s=; b=gOZkzac6I3DxDnRQOJXp6kDumY6Yhgzxm3Bww54hhnmoV9gUUzHmjuXvz++OATTkVz AqnSSwc+hgmyyb3zNCK54n+erRP1V6aq4Aa2ux5laJytg+c2FmqzJhD5tCbUMuIdwMdC cYHsuk4jVVcmFLgR2QGH/ndFyCQhbm9eVto7EleKm2uQSkahzh2+3tNSM59wBaNXII3+ ZNyvsEvGavRIRr13DcaQEJc2yhEi13gH6L7p6WOopgs2uokF3Sxfc5fwlCLwBJFloEK7 f23MA94ce08JpX+HWDVxH4M+cR1jHKmT8ezx66o3rBu/hYiUXtd/Y32Ni+8z3GA9sYEu darQ== 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=GQvUcd2bMYxPOFUAKWZ4rFYcQi8hK52tVHiFNiIyW3s=; b=FFdHReFX5ua61VmOaJnGk7ymQeN6RrEh7eFA2pXLdYcY0LViNtN/wMLZHBoeGIUs1S wr//qN9+H41VdbfBpcRpvN0NMq1szZYemEux1PxlmA4vTHa4EONFzT/tCClw8cSAReSb 5o7i6zkPvsI77pcIH2ZoSPJ4HpqjiRCEswxQAY6XFwAC4PwQpEH0WRi/n57el8ABs9gF ll3UdwlP5i18KyVuk8mUczXiIG+Nlk4aCMK3XcyRfbh1nYE81aETpyPwIXl09xDegUmZ for+jRefoqun6KaHFM77IcG70n1gbCWsh1ARd5l1/E3qj9oYrSLxXlMyimeBTL1rZEZm QyVg== X-Gm-Message-State: AOAM532Rt00cjKr1n+jN+2RQjI02sMqW7VxBwnBXGD7+xu0XDeoBZ4DQ 7N3/XLv9fTaHUevV46NB8IccEva3ejtfQDYYR1Fwe+W3HNzqyw== X-Received: by 2002:aca:1b12:: with SMTP id b18mr17594519oib.130.1620462880661; Sat, 08 May 2021 01:34:40 -0700 (PDT) MIME-Version: 1.0 References: <20210426134346.6126-1-lyl2019@mail.ustc.edu.cn> In-Reply-To: <20210426134346.6126-1-lyl2019@mail.ustc.edu.cn> From: Oded Gabbay Date: Sat, 8 May 2021 11:34:14 +0300 Message-ID: Subject: Re: [PATCH] habanalabs/gaudi: Fix a potential use after free in gaudi_memset_device_memory To: Lv Yunlong Cc: Oded Gabbay , Arnd Bergmann , Ofir Bitton , "Linux-Kernel@Vger. Kernel. Org" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 26, 2021 at 4:44 PM Lv Yunlong wrote: > > Our code analyzer reported a uaf. > > In gaudi_memset_device_memory, cb is get via hl_cb_kernel_create() > with 2 refcount. > If hl_cs_allocate_job() failed, the execution runs into release_cb > branch. One ref of cb is dropped by hl_cb_put(cb) and could be freed > if other thread also drops one ref. Then cb is used by cb->id later, > which is a potential uaf. > > My patch add a variable 'id' to accept the value of cb->id before the > hl_cb_put(cb) is called, to avoid the potential uaf. > > Fixes: 423815bf02e25 ("habanalabs/gaudi: remove PCI access to SM block") > Signed-off-by: Lv Yunlong > --- > drivers/misc/habanalabs/gaudi/gaudi.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/drivers/misc/habanalabs/gaudi/gaudi.c b/drivers/misc/habanalabs/gaudi/gaudi.c > index 9152242778f5..ecdedd87f8cc 100644 > --- a/drivers/misc/habanalabs/gaudi/gaudi.c > +++ b/drivers/misc/habanalabs/gaudi/gaudi.c > @@ -5546,6 +5546,7 @@ static int gaudi_memset_device_memory(struct hl_device *hdev, u64 addr, > struct hl_cs_job *job; > u32 cb_size, ctl, err_cause; > struct hl_cb *cb; > + u64 id; > int rc; > > cb = hl_cb_kernel_create(hdev, PAGE_SIZE, false); > @@ -5612,8 +5613,9 @@ static int gaudi_memset_device_memory(struct hl_device *hdev, u64 addr, > } > > release_cb: > + id = cb->id; > hl_cb_put(cb); > - hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb->id << PAGE_SHIFT); > + hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, id << PAGE_SHIFT); > > return rc; > } > -- > 2.25.1 > > Reviewed-by: Oded Gabbay Thanks for catching this. Applied to -fixes. Oded