Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1882026rwd; Fri, 9 Jun 2023 03:43:15 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4diFOmacr2lMZVJ5lU1Mic6x1hVwXRF1P7zYDr/BXhNr2kulJ6cm/b9FqZq0pzlLh9ZW6b X-Received: by 2002:a05:6358:4fa7:b0:123:57ad:62b6 with SMTP id cg39-20020a0563584fa700b0012357ad62b6mr964591rwb.0.1686307395177; Fri, 09 Jun 2023 03:43:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686307395; cv=none; d=google.com; s=arc-20160816; b=kCXH7fC4Hxf5r0z60K4FagiUtcG+0DXJKAmnRPU3XkIQJgj7aDAMh62E+aEpU3AqVW vHE0nhsBnSQAsH1pz6q5m5A6RAzaevoaj45GTkjm1/1sVB9RAesJpMFfdVlVwXw9odHs LqYBIJsjPBlwFl9RWLwbzAuDzWRYjIt0amBx5gMe8ABh0majJEi5Ri0wHVmdwEhW/AQt OogaSiCnMyznAJl2mKUYA4a7R4Eg/l0/crStFsLvsxahn25mJde69VMi/hqiKiPMLvSd KqxzorjpTahT4QliM26SclMNy2Cp8l2VPrwS/pe1Jsvcl5WouXrr8T3paXcoynVabqwD taGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=yS2aUgQCzMJ3EO5DLspaLc27pVQAPYGHdTdZhOt/Qbc=; b=IvUYIM00xHuH6IepnaQCmiF0n0jIDJBTENJb7Y/+czqMKkS9QhIOmlhqvBJntd95JH vrsB1ANWdOuB7VN/sakpSYhTA3YTu4ZDcrY1+eU69m9D92p0vKUSQWLfnPw0sq2ckMj3 53XItm6fPG0qeQQtY47VW53lE65RnsMMarp2TR1LFBPuhqTEBOK/XDiILJdLvVeqo9Ik kI7xLXiIBSFuWyScve2r0veLPrYUrNkPq+UBvkoiiXGQgHzDgdecl5CNwYpiOMjcl681 L3F3Lij4bUYZYW7iAQh/pMr0M4/NHOcCoAIVb2+DukyzsrpezbqRFjkJp6byLFT6Vn+z mPLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=i2R6OiNW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w8-20020a63f508000000b0053f212830aesi2399234pgh.311.2023.06.09.03.43.02; Fri, 09 Jun 2023 03:43:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=i2R6OiNW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229588AbjFIKTD (ORCPT + 99 others); Fri, 9 Jun 2023 06:19:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59506 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241345AbjFIKSG (ORCPT ); Fri, 9 Jun 2023 06:18:06 -0400 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83B3B4EE5 for ; Fri, 9 Jun 2023 03:10:35 -0700 (PDT) Received: by mail-ed1-x529.google.com with SMTP id 4fb4d7f45d1cf-514859f3ffbso2452294a12.1 for ; Fri, 09 Jun 2023 03:10:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; t=1686305434; x=1688897434; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=yS2aUgQCzMJ3EO5DLspaLc27pVQAPYGHdTdZhOt/Qbc=; b=i2R6OiNWJ812k7tEk8MFgXVAEgzGq9TX2uVlV/DmnnzCo8uf5O+u8PxJYF1fmmW7ge DiRg2DwX53b52ttYYql5uFw75qFj+mLMxZ1DBtC+j2Pp0/SnDniJ7txxwQbUhs340fZk 3ogQVRYEmLTJypVD47DipsPJixUqtHjbgHr64= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686305434; x=1688897434; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yS2aUgQCzMJ3EO5DLspaLc27pVQAPYGHdTdZhOt/Qbc=; b=VxHciWqWBqnKdSotw9PYRniyShlibMkB2EhSFI7cAmSTSljjqzvvItXEwOoA3bU5Lx QuDkIC982zRNESWH2TMyApgSck+mdYBqVVp+jim/qmaPAUzAPPWywSpbSfFL3Rd4C6PT at+/OP4Cyda0wec/00qiEnXuEMYscn1s3nTAQasPdwuXYjy6BRVmF2Y2EoM16L1cwOMU IaKv8MTXiuVC/9V5CQOWvdRTbx97pX+TqMnXxRZq2fgWGTqgyRww46ZAJuWzgYiOsfbm 91JlQJnb9KmnnqeKhXFcj+NG91LoEjaIAxq/c/yRpFOe3zNwAdFJscXn9LMZ3b7wUhY6 DRrQ== X-Gm-Message-State: AC+VfDzw60ph7NMwpHoa7JOWI7slj/jbDvDfpdWmSDAK3UM3YHqIOPST mcKBadkvwbKvakddPDKM2IV2EQ== X-Received: by 2002:a17:907:86a3:b0:974:5403:ebb with SMTP id qa35-20020a17090786a300b0097454030ebbmr1057697ejc.54.1686305433918; Fri, 09 Jun 2023 03:10:33 -0700 (PDT) Received: from [172.16.11.116] ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id ks27-20020a170906f85b00b00977eec5bb2csm1088059ejb.156.2023.06.09.03.10.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 09 Jun 2023 03:10:33 -0700 (PDT) Message-ID: <9cd596d9-0ecb-29fc-fe18-f19b86a5ba44@rasmusvillemoes.dk> Date: Fri, 9 Jun 2023 12:10:29 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: [RFC PATCH 0/1] Integer overflows while scanning for integers Content-Language: en-US, da To: Petr Mladek , 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 , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=c3=b6rn_Roy_Baron?= , Benno Lossin , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend References: <20230607223755.1610-1-richard@nod.at> <202306071634.51BBAFD14@keescook> From: Rasmus Villemoes In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 08/06/2023 17.27, Petr Mladek wrote: > On Wed 2023-06-07 16:36:12, Kees Cook wrote: > It seems that userspace implementation of sscanf() and vsscanf() > returns -ERANGE in this case. It might be a reasonable solution. Well. _Some_ userspace implementation does that. It's not in POSIX. While "man scanf" lists that ERANGE error, it also explicitly says that: CONFORMING TO The functions fscanf(), scanf(), and sscanf() conform to C89 and C99 and POSIX.1-2001. These standards do not specify the ERANGE error. I can't figure out what POSIX actually says should or could happen with sscanf("99999999999999", "%i", &x); > 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. There is absolutely no way we can start letting sscanf() return a negative err value, in exactly the same way we cannot possibly let vsnprintf() do that. We can stop early, possibly with a WARNing if it's the format string we're unhappy about ('cause that should be compile-time constant or, e.g. in the netdevice name case, carefully checked by the caller) and return "number of succesful conversions so far" (scanf) / "number of bytes written to buffer" (printf). > Alternative solution would be to update the "ip" code so that it > reads the number separately and treat zero return value as > -EINVAL. The netdev naming code _could_ be updated to just not use scanf at all or the bitmap of in-use numbers, just do the "sprintf(buf, fmt, i)" in a loop and stop when the name is not in use. That's a win as long as there are less than ~256 names already matching the pattern, but the performance absolutely tanks if there are many more than that. So I won't actually suggest that. Rasmus