From patchwork Wed Aug 15 10:19:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Pelletier X-Patchwork-Id: 10566463 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 26DAD17E1 for ; Wed, 15 Aug 2018 10:19:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 17DDD29C7B for ; Wed, 15 Aug 2018 10:19:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0BCF829CB7; Wed, 15 Aug 2018 10:19:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A166B29C7F for ; Wed, 15 Aug 2018 10:19:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729109AbeHONK5 (ORCPT ); Wed, 15 Aug 2018 09:10:57 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:33244 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729048AbeHONK5 (ORCPT ); Wed, 15 Aug 2018 09:10:57 -0400 Received: by mail-pf1-f193.google.com with SMTP id d4-v6so355853pfn.0; Wed, 15 Aug 2018 03:19:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=9HfufoAdPlu54RgqJtAj8CALr1hkk2ep8cG/qWnfY/A=; b=u72EjI1UK/X/Xh2IhBzrZdyy+DrU/eZOI0jusdwjxwJju5+aZT/XJmvgSGHNX/OnXJ cg07EMQP2P3rwu9lWGHT66skYa1I4jSloDyFf/wH6YMeHBwWKpU7VR+OtOpgR8/i9upy DcQ03oy+msI9QlHJ/gjePhYPBmGD1KS5gOTTvNWuNr8wCvx/oE74Pf/JURWP7HpGnhBr d4K75Zo/4mz55MjkzijZ6Y+5ps+45TKDPjv2Mm4WDueH1XhzcuOAgOrxw/VMGJzdyi5F AmCU5CzWS818/TEi25Ge4TEcWDNXwhiNg7np2coXIzmUumYTVeA3YLyVOhrMppeNLxFV p4RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=9HfufoAdPlu54RgqJtAj8CALr1hkk2ep8cG/qWnfY/A=; b=Va5ct/R3gixq1TZQqPjsIKVcDFZ733bldp+QCN5TWVWfCaWGLxuU+mg18qjV4dUYIo ml/loBVe61Pua0SCBOdT8mbz8AaNLCYtdzeSosRbXwwF+AzDWufaoqBuNMaAlY1JEK1k tkp6iDT9S1GuI5mUneGkPZXNUmktAQWXVvyAKxa09p1fkVjAC47yz+QozJVh7Lxp+Vbt zy4eJSkvcGZ1lRH1FIXv8pt2Z1GBatgRHzyKCfaQnR1q4thyLE7IK3PYCwBSTFh0n6FX jhVEAwHJfi8w9ALCzxBht9NXgMwzoOh5FoomIvMVrrQR7TZcwljmnjku/cRmxS74xLtA QEQw== X-Gm-Message-State: AOUpUlFSdZpI5YVIPppNGQRcdE42mwdP3gOBxv92Mtxxq+Os9jTEkPTB rKzlp2m69h8iLnIZqDjQlRw= X-Google-Smtp-Source: AA+uWPwbBKurW5fEwsPUpYomlNAyfdaEsWI28TtSPpwQcW/S5T09VJ34Q+L6LD/cHCfOoEmb571VkQ== X-Received: by 2002:a65:4304:: with SMTP id j4-v6mr25225483pgq.109.1534328362781; Wed, 15 Aug 2018 03:19:22 -0700 (PDT) Received: from x2.lan ([2400:406d:175b:7500::7d4b]) by smtp.gmail.com with ESMTPSA id d9-v6sm34303735pfb.86.2018.08.15.03.19.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 15 Aug 2018 03:19:22 -0700 (PDT) From: Vincent Pelletier To: Mike Christie Cc: Bart Van Assche , linux-scsi@vger.kernel.org, target-devel@vger.kernel.org, nab@linux-iscsi.org Subject: iscsi target: Set conn->sess to NULL when iscsi_login_set_conn_values fails Date: Wed, 15 Aug 2018 10:19:14 +0000 Message-Id: <009ea1a88311492e651c38ae946f785961a7e1a7.1534328354.git.plr.vincent@gmail.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <5B720C40.4040805@redhat.com> References: <5B720C40.4040805@redhat.com> Sender: target-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: target-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Fixes a use-after-free reported by KASAN when later iscsi_target_login_sess_out gets called and it tries to access conn->sess->se_sess: Disabling lock debugging due to kernel taint iSCSI Login timeout on Network Portal [::]:3260 iSCSI Login negotiation failed. Reviewed-by: Mike Christie ================================================================== BUG: KASAN: use-after-free in iscsi_target_login_sess_out.cold.12+0x58/0xff [iscsi_target_mod] Read of size 8 at addr ffff880109d070c8 by task iscsi_np/980 CPU: 1 PID: 980 Comm: iscsi_np Tainted: G O 4.17.8kasan.sess.connops+ #4 Hardware name: To be filled by O.E.M. To be filled by O.E.M./Aptio CRB, BIOS 5.6.5 05/19/2014 Call Trace: dump_stack+0x71/0xac print_address_description+0x65/0x22e ? iscsi_target_login_sess_out.cold.12+0x58/0xff [iscsi_target_mod] kasan_report.cold.6+0x241/0x2fd iscsi_target_login_sess_out.cold.12+0x58/0xff [iscsi_target_mod] iscsi_target_login_thread+0x1086/0x1710 [iscsi_target_mod] ? __sched_text_start+0x8/0x8 ? iscsi_target_login_sess_out+0x250/0x250 [iscsi_target_mod] ? __kthread_parkme+0xcc/0x100 ? parse_args.cold.14+0xd3/0xd3 ? iscsi_target_login_sess_out+0x250/0x250 [iscsi_target_mod] kthread+0x1a0/0x1c0 ? kthread_bind+0x30/0x30 ret_from_fork+0x35/0x40 Allocated by task 980: kasan_kmalloc+0xbf/0xe0 kmem_cache_alloc_trace+0x112/0x210 iscsi_target_login_thread+0x816/0x1710 [iscsi_target_mod] kthread+0x1a0/0x1c0 ret_from_fork+0x35/0x40 Freed by task 980: __kasan_slab_free+0x125/0x170 kfree+0x90/0x1d0 iscsi_target_login_thread+0x1577/0x1710 [iscsi_target_mod] kthread+0x1a0/0x1c0 ret_from_fork+0x35/0x40 The buggy address belongs to the object at ffff880109d06f00 which belongs to the cache kmalloc-512 of size 512 The buggy address is located 456 bytes inside of 512-byte region [ffff880109d06f00, ffff880109d07100) The buggy address belongs to the page: page:ffffea0004274180 count:1 mapcount:0 mapping:0000000000000000 index:0x0 compound_mapcount: 0 flags: 0x17fffc000008100(slab|head) raw: 017fffc000008100 0000000000000000 0000000000000000 00000001000c000c raw: dead000000000100 dead000000000200 ffff88011b002e00 0000000000000000 page dumped because: kasan: bad access detected Memory state around the buggy address: ffff880109d06f80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb ffff880109d07000: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb >ffff880109d07080: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb ^ ffff880109d07100: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc ffff880109d07180: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ================================================================== Signed-off-by: Vincent Pelletier --- drivers/target/iscsi/iscsi_target_login.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/drivers/target/iscsi/iscsi_target_login.c b/drivers/target/iscsi/iscsi_target_login.c index df81b2f7cad9..c95f4261a089 100644 --- a/drivers/target/iscsi/iscsi_target_login.c +++ b/drivers/target/iscsi/iscsi_target_login.c @@ -296,10 +296,8 @@ static int iscsi_login_zero_tsih_s1( } ret = iscsi_login_set_conn_values(sess, conn, pdu->cid); - if (unlikely(ret)) { - kfree(sess); - return ret; - } + if (unlikely(ret)) + goto free_sess; sess->init_task_tag = pdu->itt; memcpy(&sess->isid, pdu->isid, 6); sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn); @@ -333,6 +331,7 @@ static int iscsi_login_zero_tsih_s1( pr_err("idr_alloc() for sess_idr failed\n"); iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, ISCSI_LOGIN_STATUS_NO_RESOURCES); + ret = -ENOMEM; goto free_sess; } @@ -370,7 +369,7 @@ static int iscsi_login_zero_tsih_s1( free_sess: kfree(sess); conn->sess = NULL; - return -ENOMEM; + return ret; } static int iscsi_login_zero_tsih_s2(