habanalabs/gaudi: Fix a potential use after free in gaudi_memset_device_memory
authorLv Yunlong <lyl2019@mail.ustc.edu.cn>
Mon, 26 Apr 2021 13:43:46 +0000 (06:43 -0700)
committerOded Gabbay <ogabbay@kernel.org>
Sat, 8 May 2021 08:36:07 +0000 (11:36 +0300)
commit115726c5d312b462c9d9931ea42becdfa838a076
treea50f50a221cd7b6819051ec50080f2f1a7416951
parent24a107097fbd8fb6a48a0dcb31e64c1de6831a1d
habanalabs/gaudi: Fix a potential use after free in gaudi_memset_device_memory

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 <lyl2019@mail.ustc.edu.cn>
Reviewed-by: Oded Gabbay <ogabbay@kernel.org>
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
drivers/misc/habanalabs/gaudi/gaudi.c