From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F14A528F3 for ; Thu, 8 Jun 2023 15:27:46 +0000 (UTC) Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B42A172E; Thu, 8 Jun 2023 08:27:43 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 2496D1FDE6; Thu, 8 Jun 2023 15:27:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1686238062; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=YjexGMdyJWeBEjd6xCsB5dyXSs2Z5hB+eEW3odtxG74=; b=SfueeyNYTBrTihKqx8oxJyfXgJUEO72qrEaWzKN9n/xvcrRpUuC7wTlr5iQ5JU20AoKVk+ RIlBFvY+Q+Do9PokOXqyijq2TF/CAdHol/tiqVodfAgEosBNsgNhjvbDhDhwUi/3kUlSxU tJXq1cRpat2EYGkHF7TOihaLs/AntV8= Received: from suse.cz (unknown [10.100.208.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 162EF2C141; Thu, 8 Jun 2023 15:27:41 +0000 (UTC) Date: Thu, 8 Jun 2023 17:27:40 +0200 From: Petr Mladek To: Kees Cook Cc: Richard Weinberger , linux-hardening@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Steven Rostedt , Sergey Senozhatsky , Andy Shevchenko , Rasmus Villemoes , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend Subject: Re: [RFC PATCH 0/1] Integer overflows while scanning for integers Message-ID: References: <20230607223755.1610-1-richard@nod.at> <202306071634.51BBAFD14@keescook> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <202306071634.51BBAFD14@keescook> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net On Wed 2023-06-07 16:36:12, Kees Cook wrote: > On Thu, Jun 08, 2023 at 12:37:54AM +0200, Richard Weinberger wrote: > > Hi! > > > > Lately I wondered whether users of integer scanning functions check > > for overflows. > > To detect such overflows around scanf I came up with the following > > patch. It simply triggers a WARN_ON_ONCE() upon an overflow. > > > > After digging into various scanf users I found that the network device > > naming code can trigger an overflow. > > > > e.g: > > $ ip link add 1 type veth peer name 9999999999 > > $ ip link set name "%d" dev 1 > > > > It will trigger the following WARN_ON_ONCE(): > > ------------[ cut here ]------------ > > WARNING: CPU: 2 PID: 433 at lib/vsprintf.c:3701 vsscanf+0x6ce/0x990 > > Hm, it's considered a bug if a WARN or BUG can be reached from > userspace, Good point. WARN() does not look like the right way in this case. Another problem is that some users use panic_on_warn. In this case, the above "ip" command calls would trigger panic(). And it does not look like an optimal behavior. I know there already are some WARN_ONs for similar situations, e.g. set_field_width() or set_precision(). But these do not get random values. And it would actually be nice to introduce something like INFO() that would be usable for these less serious problems where the backtrace is useful but they should never trigger panic(). > so this probably needs to be rearranged (or callers fixed). > Do we need to change the scanf API for sane use inside the kernel? It seems that userspace implementation of sscanf() and vsscanf() returns -ERANGE in this case. It might be a reasonable solution. Well, there is a risk of introducing security problems. The error value might cause an underflow/overflow when the caller does not expect a negative value. Alternative solution would be to update the "ip" code so that it reads the number separately and treat zero return value as -EINVAL. Best Regards, Petr