Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2989160imu; Sun, 9 Dec 2018 14:25:33 -0800 (PST) X-Google-Smtp-Source: AFSGD/WCe3/wZ5EIvFnZOoxMXEEammnht22SViUqw8Xa1H6Y9I0P6Ls7Wvlpkj9AMTKhZXWCwdlv X-Received: by 2002:a63:5c22:: with SMTP id q34mr8774742pgb.417.1544394333724; Sun, 09 Dec 2018 14:25:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544394333; cv=none; d=google.com; s=arc-20160816; b=wVcRVQJkVnHk8QhtdU4CS/VsxKgLJePx5T/iZBOGzbDQnFMwjD7+w5QmhdvLoCSG63 QCuFdtfgSRmRIf6O+5bdtROTg90vOD+07ycl7I33Wnbp8oAJJT6b9U6/vAGd5YhkHUp7 qqmNrt+ZCyzORyRc1hX73H+ql9xss6dZyrhAuIXTXqR5eP8ZYRZKQtH95OvUgEcce+4l x5jZC5gT/qrV6XRrBdVXghVgzI9jrUVYLQMw2hzJjPOdBKRGj0nMYIBjBy+o1++HUizX M6OoXqE4grxnq6HX3IHe09HtW3FGUmqzyp0L1PnaIS39pFma5Gw59f6yHjIf3oU/S9fu l0XQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=NkFh1pI7FmXsBfMwN59Y9otILQdzZPFv7uWSmeDbXV4=; b=hM3cZTG1bdo2Vx7aklGmzeYwp5H/j1LDyvoDsrP6yv7NakVWvaqiiwkluUWBa9mZFR qF08g31B7A2dO2KkkuljQ5RhGjAvTukguxcXIkETyPRC/o4XMaAuCe8/pugCohi+PV4S VKHQ/mdnRMEFLGvFojzFlL7mbEXKYojYSG5+zI1X+mxiv0N9A3Du1xOiuHnWDjq4O0qy 1PP2sP1YMMEX9BSCDlSa9lQUEyIL5FLAET8/78hMUD+5Plbcfr+GPexL2iaMEkqJOD1P KJNwQx82lSQOo8Xw+uTHFQT41pV/GCowkv0+NAfvdUJ+Rj5vpa6R16jeh15KBMaFibq1 12oQ== 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 j20si7342129pgg.162.2018.12.09.14.25.18; Sun, 09 Dec 2018 14:25:33 -0800 (PST) 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 S1728062AbeLIWWv (ORCPT + 99 others); Sun, 9 Dec 2018 17:22:51 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:35210 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726518AbeLIVzV (ORCPT ); Sun, 9 Dec 2018 16:55:21 -0500 Received: from pub.yeoldevic.com ([81.174.156.145] helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gW72l-0002il-5F; Sun, 09 Dec 2018 21:55:19 +0000 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1gW72h-0003ZL-Sz; Sun, 09 Dec 2018 21:55:15 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Steve French" , "Pavel Shilovsky" , "Aurelien Aptel" , "Dan Carpenter" Date: Sun, 09 Dec 2018 21:50:33 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) X-Patchwork-Hint: ignore Subject: [PATCH 3.16 237/328] CIFS: fix wrapping bugs in num_entries() In-Reply-To: X-SA-Exim-Connect-IP: 81.174.156.145 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.62-rc1 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 Signed-off-by: Ben Hutchings --- fs/cifs/smb2pdu.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) --- a/fs/cifs/smb2pdu.c +++ b/fs/cifs/smb2pdu.c @@ -2148,33 +2148,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; }