From patchwork Fri Dec 23 07:37:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaoqian Lin X-Patchwork-Id: 36155 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp192302wrn; Thu, 22 Dec 2022 23:50:07 -0800 (PST) X-Google-Smtp-Source: AMrXdXt/l/Krid0ItiaPfbSn2SDKKTrAtHVeqg8tfTew+YSESsSmPkdBl0KSbMIG8QCA0STqa44S X-Received: by 2002:a17:906:7188:b0:7c0:f117:6990 with SMTP id h8-20020a170906718800b007c0f1176990mr7187397ejk.41.1671781807201; Thu, 22 Dec 2022 23:50:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671781807; cv=none; d=google.com; s=arc-20160816; b=XFq6PXmsMDpMYRYZH8+rwytj+tS1TBblGnphwzMPgj1ptEtxvQbnU8X480/fdICyCO eJ//V6NFQYvhcEwYrVwPwWraVRvaDeKyDZ/3Njvh6s7SLSAyPAF+lxR+mrA3XBoJE1Ga jWdySGz9p2LbzZKCeqYH8BkA8ZZH8tqHgMq0wiEjgBWRf0TmjzMmDlbf1FfIVBd2OykM Ee5cB29T3LGNJ8X+qq9STiWG6R/OFBZZBMuRt2xSc/LiI4AuzDU/q/vMTbAxHVbrz2jg JPrVhX5nhlJAK4xxVKN4i2v2ITfVr3pUUf/srzIFgFvkOa3s/5sEX5RyNXQMSt+4TNcO Py/A== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=r0Pq6z8DEwQueVzvyIU52Z0rGP0y+vzxROpohdrh7mI=; b=PX2X3eMbzRxkZU1Aof9eZ9wRn+Wzd79kWWfPaONvecHCkFyVGnFyQGvDy6GpTfsv7F OdM49LgpykG/zcP2RsJUFwGHdut59la/8zU+gVo1E2dSmywyCUiqAfF0jbwPYu1ak36f HVjMYhjLDPHox3Cej5AVSrYE1XbJNvfBCPulBxUIyBszxLeCF2wPccFIlaTbXApb/E3X yMDn4lWXUWGsWtInKrL7j8HS9t0Zy41tnqhKq1091BXs/dCrnEchmpavbvEB3DZCFqOK 0YBpOUNgiSWLkbG842Z/fVR1YOXCZ2/fMq6dhzIhE8Tn3YZxodYPGHo+U5qInelJ0ax5 CfRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=RAmqRIPZ; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jg14-20020a170907970e00b007c1651709e3si2321657ejc.177.2022.12.22.23.49.42; Thu, 22 Dec 2022 23:50:07 -0800 (PST) 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=@gmail.com header.s=20210112 header.b=RAmqRIPZ; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235701AbiLWHhc (ORCPT + 99 others); Fri, 23 Dec 2022 02:37:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229897AbiLWHh3 (ORCPT ); Fri, 23 Dec 2022 02:37:29 -0500 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE97F12A9E; Thu, 22 Dec 2022 23:37:28 -0800 (PST) Received: by mail-pj1-x102f.google.com with SMTP id w4-20020a17090ac98400b002186f5d7a4cso8135666pjt.0; Thu, 22 Dec 2022 23:37:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=r0Pq6z8DEwQueVzvyIU52Z0rGP0y+vzxROpohdrh7mI=; b=RAmqRIPZVFG60+za8g3yVZgw9P4SgIhwJfXhfqulZWMbOs6uPyJ9qtBHX76U1Es031 puH5T06b72iVSZtI+e6pAi8jbwNVzKD3JdoRdSZ4ggbM1+WPhsSHQL8CEsOGzzq8KtaI 9GSdCSetpy/KZuDOcmr17g8dzCrXjrsBlqkWdy4U8p0P74Xh0ybywNEZOJPrFp4B1thZ lQ9Sk4ZFv7cPMGvVRl9NCX3GlHE2DxjR8jaXcD8DPh0hYuLGhmR+EdNuvaB80vEO4Kkv 2l+1MHuJu1iZWrEZnvjlFvb4OsQYpVYXs6s2fTzsnSr5gB8T4MS6cfWTTpgVXI99Ywi/ pl1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=r0Pq6z8DEwQueVzvyIU52Z0rGP0y+vzxROpohdrh7mI=; b=eL64kFJGdiTCpsGzhGuZ2OnxonZUnEaK2/EjRWp32DKM5dKhKjJbuKrqxPZdrdiLH4 YitEbW/nUoskhsT38pJ1zLzYHU3bs5Xa87IGuzZe/+KJ37ndXddfDCT7s9hycboY901x va9gApe/DBQObPZ+dVufpGowrbsfJa8oijp1y+RRmX77i3KGHy5mOptsSz95EvO31+tw 0+8kYal3c0vMCV6UW8hN05bD2qjqye8PuqqTxRdMiVknhtpI39siizZvaKFbR7ZZamfA bbMnw8xDBciVHSND7Ddyz3ZZo5RaqiBXAvj23UMLhR5MAyouvsscfSZLd1BUYPacVXpQ T9rQ== X-Gm-Message-State: AFqh2koIVoviWSfHS1dLOE4/+361DiOophRTUBeIK+UdE/jrFD76ZF2D OJUaNlR7c//Fg/LDwoBn8tf2ug5f21PbhdaV X-Received: by 2002:a17:90a:f614:b0:219:672a:42db with SMTP id bw20-20020a17090af61400b00219672a42dbmr10443032pjb.19.1671781048176; Thu, 22 Dec 2022 23:37:28 -0800 (PST) Received: from localhost.localdomain ([202.120.234.246]) by smtp.googlemail.com with ESMTPSA id qe12-20020a17090b4f8c00b00212cf2fe8c3sm7271669pjb.1.2022.12.22.23.37.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Dec 2022 23:37:27 -0800 (PST) From: Miaoqian Lin To: Krzysztof Kozlowski , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Samuel Ortiz , Christophe Ricard , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: linmq006@gmail.com Subject: [PATCH v2] nfc: Fix potential resource leaks Date: Fri, 23 Dec 2022 11:37:18 +0400 Message-Id: <20221223073718.805935-1-linmq006@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752990280281247018?= X-GMAIL-MSGID: =?utf-8?q?1752990280281247018?= nfc_get_device() take reference for the device, add missing nfc_put_device() to release it when not need anymore. Also fix the style warnning by use error EOPNOTSUPP instead of ENOTSUPP. Fixes: 5ce3f32b5264 ("NFC: netlink: SE API implementation") Fixes: 29e76924cf08 ("nfc: netlink: Add capability to reply to vendor_cmd with data") Signed-off-by: Miaoqian Lin --- changes in v2: - rename error to rc. - add blank line. v1: https://lore.kernel.org/all/20221220134623.2084443-1-linmq006@gmail.com/ --- net/nfc/netlink.c | 52 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/net/nfc/netlink.c b/net/nfc/netlink.c index 9d91087b9399..1fc339084d89 100644 --- a/net/nfc/netlink.c +++ b/net/nfc/netlink.c @@ -1497,6 +1497,7 @@ static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info) u32 dev_idx, se_idx; u8 *apdu; size_t apdu_len; + int rc; if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || !info->attrs[NFC_ATTR_SE_INDEX] || @@ -1510,25 +1511,37 @@ static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info) if (!dev) return -ENODEV; - if (!dev->ops || !dev->ops->se_io) - return -ENOTSUPP; + if (!dev->ops || !dev->ops->se_io) { + rc = -EOPNOTSUPP; + goto put_dev; + } apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]); - if (apdu_len == 0) - return -EINVAL; + if (apdu_len == 0) { + rc = -EINVAL; + goto put_dev; + } apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]); - if (!apdu) - return -EINVAL; + if (!apdu) { + rc = -EINVAL; + goto put_dev; + } ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL); - if (!ctx) - return -ENOMEM; + if (!ctx) { + rc = -ENOMEM; + goto put_dev; + } ctx->dev_idx = dev_idx; ctx->se_idx = se_idx; - return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx); + rc = nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx); + +put_dev: + nfc_put_device(dev); + return rc; } static int nfc_genl_vendor_cmd(struct sk_buff *skb, @@ -1551,14 +1564,21 @@ static int nfc_genl_vendor_cmd(struct sk_buff *skb, subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]); dev = nfc_get_device(dev_idx); - if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds) + if (!dev) return -ENODEV; + if (!dev->vendor_cmds || !dev->n_vendor_cmds) { + err = -ENODEV; + goto put_dev; + } + if (info->attrs[NFC_ATTR_VENDOR_DATA]) { data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]); data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]); - if (data_len == 0) - return -EINVAL; + if (data_len == 0) { + err = -EINVAL; + goto put_dev; + } } else { data = NULL; data_len = 0; @@ -1573,10 +1593,14 @@ static int nfc_genl_vendor_cmd(struct sk_buff *skb, dev->cur_cmd_info = info; err = cmd->doit(dev, data, data_len); dev->cur_cmd_info = NULL; - return err; + goto put_dev; } - return -EOPNOTSUPP; + err = -EOPNOTSUPP; + +put_dev: + nfc_put_device(dev); + return err; } /* message building helper */