Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2509887imm; Mon, 24 Sep 2018 05:40:58 -0700 (PDT) X-Google-Smtp-Source: ACcGV63bY/A5Ohek5i0JzF0e+dCmMLXEbOfbjlwqBMeYMvkI81MYjfzoTPOMK9205k1lZxmflS7S X-Received: by 2002:a17:902:8c90:: with SMTP id t16-v6mr10272153plo.251.1537792858788; Mon, 24 Sep 2018 05:40:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537792858; cv=none; d=google.com; s=arc-20160816; b=gVmCKWiwtMHuytKlt/qVpI8fgGrJ+Edn1vTEt4K6LcLuXF9iIbVbjAmCV2cijbQHWW zbqUzP2zjim2Vy1tX/1YcjD3nbSSJsNYiKGa5j6DyPriNAPhCUY0lZ/wS3hI9lQaqFwW ZvI+QmdnBpdY+zz/RXknCtrN3gEfYAXzK1Rmv0g11aRARn6J51+RBTZ2QUNS0vufx3Sm nnfoNM/ORs65hAv1lRqzVOZ1y00OV4d68V2MOkMIKcChh8bzbzdbUAvt8JB2YUJ0bENm S9pooMCfb7JpI5wJxOh7h8ZUA21RrV/eR+YNSDNcYrpZtnb0fZCoY7UXXUlbNZO1XrJ4 vKJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from; bh=XYA94JVCXTlm3eNFGteP8zwfPH4cjyLryz1lRxVM6C0=; b=HAOwB5c/Qp05J/xhClLbaoKbHYHcLhcSurBqkCrHB2FxBQVnizDQLYOcsJGqOgqS8/ TC/hSvYaOI7cdjaJ/N26oYC8mEmJpnyUVi4OTfvnw799dN5YqW75hWLRNocvbefBqhGF lSx1t0uXWK2KgnmurVJ4iSe21ApPWKHvDyMbn9OtC7itA6JXWpggYXOZq6pmO1CFEpNy NG8WKHrUx1BDzwNDeQ3KLv1x0T6V7inXfbaXbBqZRna/XKp+GNvbxhBmB9rWbpFCZYtK 3Kk8v9IxmEGBJfg0MB1wyFuVxeauhUDhioxs/iXrTjfecgShPf3hzTLhKFyDFk6FBF1Q 6xuA== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p187-v6si38282826pfp.27.2018.09.24.05.40.43; Mon, 24 Sep 2018 05:40:58 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388102AbeIXSkW (ORCPT + 99 others); Mon, 24 Sep 2018 14:40:22 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:58712 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728702AbeIXSkV (ORCPT ); Mon, 24 Sep 2018 14:40:21 -0400 Received: from localhost (ip-213-127-77-73.ip.prioritytelecom.net [213.127.77.73]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 11DED1097; Mon, 24 Sep 2018 12:38:23 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dan Carpenter , Steve French , Aurelien Aptel , Pavel Shilovsky Subject: [PATCH 4.18 135/235] CIFS: fix wrapping bugs in num_entries() Date: Mon, 24 Sep 2018 13:52:01 +0200 Message-Id: <20180924113119.288427413@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180924113103.999624566@linuxfoundation.org> References: <20180924113103.999624566@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Dan Carpenter commit 56446f218af1133c802dad8e9e116f07f381846c upstream. The problem is that "entryptr + next_offset" and "entryptr + len + size" can wrap. I ended up changing the type of "entryptr" because it makes the math easier when we don't have to do so much casting. Signed-off-by: Dan Carpenter Signed-off-by: Steve French Reviewed-by: Aurelien Aptel Reviewed-by: Pavel Shilovsky CC: Stable Signed-off-by: Greg Kroah-Hartman --- fs/cifs/smb2pdu.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) --- a/fs/cifs/smb2pdu.c +++ b/fs/cifs/smb2pdu.c @@ -3492,33 +3492,38 @@ num_entries(char *bufstart, char *end_of int len; unsigned int entrycount = 0; unsigned int next_offset = 0; - FILE_DIRECTORY_INFO *entryptr; + char *entryptr; + FILE_DIRECTORY_INFO *dir_info; if (bufstart == NULL) return 0; - entryptr = (FILE_DIRECTORY_INFO *)bufstart; + entryptr = bufstart; while (1) { - entryptr = (FILE_DIRECTORY_INFO *) - ((char *)entryptr + next_offset); - - if ((char *)entryptr + size > end_of_buf) { + if (entryptr + next_offset < entryptr || + entryptr + next_offset > end_of_buf || + entryptr + next_offset + size > end_of_buf) { cifs_dbg(VFS, "malformed search entry would overflow\n"); break; } - len = le32_to_cpu(entryptr->FileNameLength); - if ((char *)entryptr + len + size > end_of_buf) { + entryptr = entryptr + next_offset; + dir_info = (FILE_DIRECTORY_INFO *)entryptr; + + len = le32_to_cpu(dir_info->FileNameLength); + if (entryptr + len < entryptr || + entryptr + len > end_of_buf || + entryptr + len + size > end_of_buf) { cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n", end_of_buf); break; } - *lastentry = (char *)entryptr; + *lastentry = entryptr; entrycount++; - next_offset = le32_to_cpu(entryptr->NextEntryOffset); + next_offset = le32_to_cpu(dir_info->NextEntryOffset); if (!next_offset) break; }