From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753990AbbFEAgl (ORCPT ); Thu, 4 Jun 2015 20:36:41 -0400 Received: from zeniv.linux.org.uk ([195.92.253.2]:56193 "EHLO ZenIV.linux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752184AbbFEAgj (ORCPT ); Thu, 4 Jun 2015 20:36:39 -0400 Date: Fri, 5 Jun 2015 01:36:35 +0100 From: Al Viro To: Helge Deller Cc: Linus Torvalds , linux-kernel@vger.kernel.org Subject: Re: [PATCH] compat: fix possible out-of-bound accesses in compat_get_bitmap() and compat_put_bitmap() Message-ID: <20150605003635.GQ7232@ZenIV.linux.org.uk> References: <20150601184437.GA2534@ls3530.box> <55705670.9000808@gmx.de> <20150604220743.GA9221@ls3530.box> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20150604220743.GA9221@ls3530.box> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 05, 2015 at 12:07:43AM +0200, Helge Deller wrote: > In the functions compat_get_bitmap() and compat_put_bitmap() the variable > nr_compat_longs stores how many compat_ulong_t words should be copied in a > loop. > > The copy loop itself is this: > if (nr_compat_longs-- > 0) { > if (__get_user(um, umask)) return -EFAULT; > } else { > um = 0; > } Er... When does that condition trigger? We start with (((n)+BITS_PER_COMPAT_LONG-1)/BITS_PER_COMPAT_LONG), which is essentially DIV_ROUND_UP(n, BITS_PER_BYTE * sizeof(compat_long_t)). Then we go through DIV_ROUND_UP(n, BITS_PER_BYTE * sizeof(long)) iterations of outer loop, with sizeof(long)/sizeof(compat_long_t) iterations on inner one every time. So basically that thing will trigger only on the last pass through the outer loop. The only way for it to trigger a wraparound would be to have sizeof(long)/sizeof(compat_long_t) greater than LONG_MAX, which is, not too likely. If we decrement nr_compat_longs and want to stop when it reaches zero, why not use that as the (only) loop condition? As in for (m = 0, shift = 0; nr_compat_longs--;) { compat_ulong_t um; if (__get_user(um, umask++)) return -EFAULT; m |= (long)um << shift; shift += BITS_PER_COMPAT_LONG; if (shift == BITS_PER_LONG) { shift = 0; *mask++ = m; m = 0; } } if (shift) *mask++ = m;