Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2899799imm; Tue, 4 Sep 2018 11:49:07 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZI8TOuv70lH+TIGQFSQnJdxbWywNA5theZkd6itD9FOhjEJx6Cb/WwyRVoJxyPuhERgCRz X-Received: by 2002:a17:902:b60e:: with SMTP id b14-v6mr34761394pls.111.1536086947605; Tue, 04 Sep 2018 11:49:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536086947; cv=none; d=google.com; s=arc-20160816; b=oIjHG9DnQHzQydmLh9BSCHoeag68F47f7ciQw2h39wM8LAgzI/ncOvZ5TkYJn+q6Ij h4gorSWJ3/+PYgB1c4TdtLw44zlRSI1JF2CkEcGkgCi8zV9N+xwTWefP+vvvP1F3Fi4s PuUJHmAPBKDAYIfSfQwvGy2kYsxO2BUWFlEGB2GSb3V9QXbL3o3GEKJyHNACdD5qaRFk 0RUiO4NLpbLqkwgoCVWqS0aeLELRLWqgdRUltQxls3Q8HbtauucmGULGt4BNGATo3rGv KrWTIVHEGjsH3wKmPT3rcYEPpsH8EVeII1NnkSdfeIoYYJygxJ3xSSi9q304QNv0dVNM G0dQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=uNMTaD34cNXpuKyXxVIDAmgW5Xj/HNcF73LOfiYXYV4=; b=pOO/Z5Z+5jscNZfzLwy496GEvmL+nkTirzjeGxAFB6W9svNiKXGDOULdkVvb1D6JVd w/l+h/AKWNXNzPtjQ7zakQCI4dQTnup4Vky64nwvHXV235Fr+8duMUNgAMirmfltDSRb tFDuog2LyS73dRLqh7tt0/s3XXDH9wIBxWivsqIxKtCmvx80NAk7QX/jbgnPBJkwe1CD 5FnRQGgPVbNtX5wYTrj3IBOvlDmmJ6FkoSWP0LP9v9xCII4AycOwWmh2ybU+XyjtBLaG 4z9aUHEWJRegs59dqIyEUPtI9BwmpBYlNHp8VoQ0QfTTeyqM4NCPmVyC73/wSpHVnjV5 DVEQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q8-v6si19346065pls.482.2018.09.04.11.48.52; Tue, 04 Sep 2018 11:49:07 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727688AbeIDXOJ (ORCPT + 99 others); Tue, 4 Sep 2018 19:14:09 -0400 Received: from mail-qt0-f196.google.com ([209.85.216.196]:46046 "EHLO mail-qt0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726708AbeIDXOJ (ORCPT ); Tue, 4 Sep 2018 19:14:09 -0400 Received: by mail-qt0-f196.google.com with SMTP id g44-v6so5166961qtb.12 for ; Tue, 04 Sep 2018 11:47:46 -0700 (PDT) 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; bh=uNMTaD34cNXpuKyXxVIDAmgW5Xj/HNcF73LOfiYXYV4=; b=fKkP37dwHNJ9mX5kppZhdd9XKv+AGAF09QHwFUL9+M0Y3VtFFenPKb8JwcxwQql2Fj y0vABmAydaE7efTe6gx6ydR+9q98fZA1Tgzxgori1z7zjCmVuuK07j73Z6B5txxn4HNH lMTJbMt8t6THWORa3dHNCuLrUTKexQFmPUgvRaKOYrZXHqhoFQRsUVrtLj60aLzNT2NR aSvsTuAF1gSfnU3zsRnThCc9X0hxmuv57+B63Tnqvz+Xnr7ijZl+EgBYuV/rZab8Hi2g Dmhc65HhxyLR7aPPJC6jzchP2RlBFMyFSGjRhY8R1y32tuwaQbi7+5Hyk8iwc5ZmKaU3 fuMA== X-Gm-Message-State: APzg51DrZwlUyvugsIOefONOi6x2fEeUzDxjwsD2JnSnl4D85P3QnPIq 8zwYQB+x4/+X4i0n+OlHPbLqJQ== X-Received: by 2002:ac8:167a:: with SMTP id x55-v6mr31090803qtk.67.1536086866482; Tue, 04 Sep 2018 11:47:46 -0700 (PDT) Received: from labbott-redhat.redhat.com ([2601:602:9802:a8dc::3c7a]) by smtp.gmail.com with ESMTPSA id f63-v6sm12601473qtb.64.2018.09.04.11.47.43 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 04 Sep 2018 11:47:45 -0700 (PDT) From: Laura Abbott To: "Nicholas A. Bellinger" , Bart Van Assche , Hannes Reinecke , Mike Christie , Maurizio Lombardi Cc: Laura Abbott , target-devel@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , "Martin K . Petersen" Subject: [PATCHv3] iscsi-target: Don't use stack buffer for scatterlist Date: Tue, 4 Sep 2018 11:47:40 -0700 Message-Id: <20180904184740.11102-1-labbott@redhat.com> X-Mailer: git-send-email 2.17.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Fedora got a bug report of a crash with iSCSI: kernel BUG at include/linux/scatterlist.h:143! ... RIP: 0010:iscsit_do_crypto_hash_buf+0x154/0x180 [iscsi_target_mod] ... Call Trace: ? iscsi_target_tx_thread+0x200/0x200 [iscsi_target_mod] iscsit_get_rx_pdu+0x4cd/0xa90 [iscsi_target_mod] ? native_sched_clock+0x3e/0xa0 ? iscsi_target_tx_thread+0x200/0x200 [iscsi_target_mod] iscsi_target_rx_thread+0x81/0xf0 [iscsi_target_mod] kthread+0x120/0x140 ? kthread_create_worker_on_cpu+0x70/0x70 ret_from_fork+0x3a/0x50 This is a BUG_ON for using a stack buffer with a scatterlist. There are two cases that trigger this bug. Switch to using a dynamically allocated buffer for one case and do not assign a NULL buffer in another case. Signed-off-by: Laura Abbott --- v3: Switched to kcalloc to drop an extra memset call. I was asked offline about kmalloc vs. kmalloc_array and ended up asking about this at the security summit since I was there last week. The preferred style is kmalloc_array for all arrays, even those of one byte characters. I will also switch it if people feel really strongly about this or I've misunderstood the array guidelines. --- drivers/target/iscsi/iscsi_target.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c index 94bad43c41ff..2a586b58a011 100644 --- a/drivers/target/iscsi/iscsi_target.c +++ b/drivers/target/iscsi/iscsi_target.c @@ -1416,7 +1416,8 @@ static void iscsit_do_crypto_hash_buf(struct ahash_request *hash, sg_init_table(sg, ARRAY_SIZE(sg)); sg_set_buf(sg, buf, payload_length); - sg_set_buf(sg + 1, pad_bytes, padding); + if (padding) + sg_set_buf(sg + 1, pad_bytes, padding); ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding); @@ -3910,10 +3911,14 @@ static bool iscsi_target_check_conn_state(struct iscsi_conn *conn) static void iscsit_get_rx_pdu(struct iscsi_conn *conn) { int ret; - u8 buffer[ISCSI_HDR_LEN], opcode; + u8 *buffer, opcode; u32 checksum = 0, digest = 0; struct kvec iov; + buffer = kcalloc(ISCSI_HDR_LEN, sizeof(*buffer), GFP_KERNEL); + if (!buffer) + return; + while (!kthread_should_stop()) { /* * Ensure that both TX and RX per connection kthreads @@ -3921,7 +3926,6 @@ static void iscsit_get_rx_pdu(struct iscsi_conn *conn) */ iscsit_thread_check_cpumask(conn, current, 0); - memset(buffer, 0, ISCSI_HDR_LEN); memset(&iov, 0, sizeof(struct kvec)); iov.iov_base = buffer; @@ -3930,7 +3934,7 @@ static void iscsit_get_rx_pdu(struct iscsi_conn *conn) ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN); if (ret != ISCSI_HDR_LEN) { iscsit_rx_thread_wait_for_tcp(conn); - return; + break; } if (conn->conn_ops->HeaderDigest) { @@ -3940,7 +3944,7 @@ static void iscsit_get_rx_pdu(struct iscsi_conn *conn) ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN); if (ret != ISCSI_CRC_LEN) { iscsit_rx_thread_wait_for_tcp(conn); - return; + break; } iscsit_do_crypto_hash_buf(conn->conn_rx_hash, buffer, @@ -3964,7 +3968,7 @@ static void iscsit_get_rx_pdu(struct iscsi_conn *conn) } if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT) - return; + break; opcode = buffer[0] & ISCSI_OPCODE_MASK; @@ -3975,13 +3979,15 @@ static void iscsit_get_rx_pdu(struct iscsi_conn *conn) " while in Discovery Session, rejecting.\n", opcode); iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, buffer); - return; + break; } ret = iscsi_target_rx_opcode(conn, buffer); if (ret < 0) - return; + break; } + + kfree(buffer); } int iscsi_target_rx_thread(void *arg) -- 2.17.1