Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp2126417rwe; Fri, 2 Sep 2022 08:54:34 -0700 (PDT) X-Google-Smtp-Source: AA6agR5ZT9fAsnvZvq/IDFk5ZFnm/tEivnsiz4MgvVVTDaVa1E3qKBQyqkpUfEe26Agl9mciP6Yh X-Received: by 2002:a17:907:7284:b0:731:82a3:16e3 with SMTP id dt4-20020a170907728400b0073182a316e3mr27704509ejc.30.1662134074105; Fri, 02 Sep 2022 08:54:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662134074; cv=none; d=google.com; s=arc-20160816; b=U6sOd/Wmyy95pWakXCgCDxJGzYuqc4CRCVjCjA8m0KHQJZTldrFKDGjPsA51vl2G8y JEYzkz51SmAeiqgXWOusT+e2aF+haOB8C9gu29mboCbu/2894W0WFiQ2snR6TqpRo1xx 1eUCLb97BE9Nx+v3NgSDv+4MqlaIVwV0YWwcbEHVGLdSeeM7EbtPEAyrJ0df4DSxiJsw gyRuitJhHyGuuKA6dS6ZsH1cpDMp83oVJbP5sYJLY8QiM+v9sQ967qvor/s1w37ILDJd JadArCz6jxxwei8HTuP2UiQGk0HK+rbZCUng7xBgHyGpFLIAZl0sBYXB93dFB1K+xbCu iV6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=vho+m1JeucUWDUID2NTMp/Noqso8ylqG8qhETHGnWbE=; b=EaiFDA1/g/Y05FKh+r1HJpm6IVc88cFL37/CKTTac7EOsxnLbvcfolSfCN+eXmRkQg 67DoMhoBcO5WlmUCh8TyjtUo1GvHIkpseWMHO1LvQ98yVwldIVNZZXFli7dSCzkl4RQd eb7Awv38b24WuykyBYZGxJZlw0OoRgr5CoPCTUcx85rsyrJLx18jiTA3Tuf0vWmDrCa7 a2GyMrr6lvAgUDlDL1BKQhew/xjaro9pYB+GBQWMilAILAN5xUax5N2dcoYzo0qUU6XC LOyH2Cc0BiwYff+GWE62cot/C2gomi5sTGV041M8lcVbljDr46BeujeTTiPbNVfPj3Cg DjTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=QVOiQ2e7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ga13-20020a1709070c0d00b00741362a9695si2612610ejc.13.2022.09.02.08.54.07; Fri, 02 Sep 2022 08:54:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=QVOiQ2e7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236618AbiIBPcv (ORCPT + 99 others); Fri, 2 Sep 2022 11:32:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236923AbiIBPcZ (ORCPT ); Fri, 2 Sep 2022 11:32:25 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DEEB41BE for ; Fri, 2 Sep 2022 08:15:02 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id j26so1449469wms.0 for ; Fri, 02 Sep 2022 08:15:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=vho+m1JeucUWDUID2NTMp/Noqso8ylqG8qhETHGnWbE=; b=QVOiQ2e7b4P6KA2s1KjnsxNEuUTskSx/mRNh8xvVVJe7CdZqbZqcy0gJl4S6H9za6D yF8sX0J/6Vu3KQYvuMWTkOvGNW5WsIqHfLqk51R1wijjiAGl7b8oqNIE+OOmXKMqsTS2 XClwgCJhR5GazQ1izvnAKX5Qa1vjJ2A6RLSNVSYvhyeZTQeTeodPSiCfPatk9SqzEKiy Kh4NXr1RWZu/WKw3M3HhVI8IBfF7Bm7CqF14G1rzwK7dVHPqzNqId9I0W2EDz4sDUMYV sJTG+sJNnfxpWKSVwQeUAUZ1Pu5mVFoNfHvptmsq+2DrzqpqjWqHqStXjOwB5OdDoF2i WlhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=vho+m1JeucUWDUID2NTMp/Noqso8ylqG8qhETHGnWbE=; b=5T6pv5M1t2rMczBK+7NFGcvKTHBTZOZtDGEFQpXVx2tcMww+RrPizrtQXV6MgLbzZh oNFvLckhqMpDPvUrbY/jJgA+WOUhidMfLWRT8nOJlv/1K4yx64cZmeYNZcM/uI16gSRH 5qUJWfrX6b6jXNpcJ55NxnZ0B0cGEtqXOaU+lG5AoVbRXq2i69K3yMH6uQT1SzsVazYi rJRMW+cqrjKz8Gc0e8gNDMWQH0ETWq2lq9awpYsxAaKXK4ynodjZ4aMuyN0XBjXfXTS/ Xr6G6asIOjWubx2JqKuU9AX0QIgbmRm1kf4KSkJD6ry2MnZjpfUmiyDX05OB2oPHpihC 9lhA== X-Gm-Message-State: ACgBeo2DSHG6YObIdDwkLBtb23nlhI/8hCLZ3y2Bd9+EZMRwOsRejYp7 eYn0r9SiuRO/gK2nWd/34fU2KA== X-Received: by 2002:a7b:cd11:0:b0:3a8:3f6c:9abf with SMTP id f17-20020a7bcd11000000b003a83f6c9abfmr3299238wmj.30.1662131700793; Fri, 02 Sep 2022 08:15:00 -0700 (PDT) Received: from hackbox.lan ([94.52.112.99]) by smtp.gmail.com with ESMTPSA id w10-20020a05600c014a00b003a4efb794d7sm2361493wmm.36.2022.09.02.08.14.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Sep 2022 08:15:00 -0700 (PDT) From: Abel Vesa To: Srinivas Kandagatla , Amol Maheshwari , Ekansh Gupta , Vamsi Krishna Gattupalli Cc: Arnd Bergmann , Greg Kroah-Hartman , linux-arm-msm@vger.kernel.org, Linux Kernel Mailing List , Ola Jeppsson Subject: [PATCH 2/3] misc: fastrpc: Don't remove map on creater_process and device_release Date: Fri, 2 Sep 2022 18:14:22 +0300 Message-Id: <20220902151423.3351414-2-abel.vesa@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220902151423.3351414-1-abel.vesa@linaro.org> References: <20220902151423.3351414-1-abel.vesa@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Do not remove the map from the list on error path in fastrpc_init_create_process, instead call fastrpc_map_put, to avoid use-after-free. Do not remove it on fastrpc_device_release either, call fastrpc_map_put instead. The fastrpc_free_map is the only proper place to remove the map. This is called only after the reference count is 0. Fixes: b49f6d83e290f ("misc: fastrpc: Fix a possible double free") Co-developed-by: Ola Jeppsson Signed-off-by: Ola Jeppsson Signed-off-by: Abel Vesa --- drivers/misc/fastrpc.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c index 0c816a11eeec..50c17f5da3a8 100644 --- a/drivers/misc/fastrpc.c +++ b/drivers/misc/fastrpc.c @@ -316,6 +316,13 @@ static void fastrpc_free_map(struct kref *ref) dma_buf_put(map->buf); } + if (map->fl) { + spin_lock(&map->fl->lock); + list_del(&map->node); + spin_unlock(&map->fl->lock); + map->fl = NULL; + } + kfree(map); } @@ -1266,12 +1273,7 @@ static int fastrpc_init_create_process(struct fastrpc_user *fl, fl->init_mem = NULL; fastrpc_buf_free(imem); err_alloc: - if (map) { - spin_lock(&fl->lock); - list_del(&map->node); - spin_unlock(&fl->lock); - fastrpc_map_put(map); - } + fastrpc_map_put(map); err: kfree(args); @@ -1347,10 +1349,8 @@ static int fastrpc_device_release(struct inode *inode, struct file *file) fastrpc_context_put(ctx); } - list_for_each_entry_safe(map, m, &fl->maps, node) { - list_del(&map->node); + list_for_each_entry_safe(map, m, &fl->maps, node) fastrpc_map_put(map); - } list_for_each_entry_safe(buf, b, &fl->mmaps, node) { list_del(&buf->node); -- 2.34.1