From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.2 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 126B8C4707F for ; Thu, 27 May 2021 23:01:38 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D5DEF61181 for ; Thu, 27 May 2021 23:01:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D5DEF61181 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=prevas.dk Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 308CB82CFA; Fri, 28 May 2021 01:01:32 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=prevas.dk Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=prevas.dk header.i=@prevas.dk header.b="cmh6BdbA"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 7CB8382D14; Fri, 28 May 2021 01:01:30 +0200 (CEST) Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on20716.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e1b::716]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 49A7681DB3 for ; Fri, 28 May 2021 01:01:27 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=prevas.dk Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=rasmus.villemoes@prevas.dk ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PGc7whbHCfFImyqcNOo/LqPlYevE6hP5slVvUqVxW6/19UOeboBRZFmeMlhzLDjK+mioiOtlqS6B+ZrnKJ81pp5Gy4zkooIg7/aQM1VRQbSDvj5/PlFTTPqNQEGrEREEsRyxHrkK2211eEegLLcuIxuiCw48o2otNalQUBWDt7WnfU+CwSYEp6WDRCQ+ed7qk/heTM5mY/BYEfpWAU9HqIoWRgFOGz042evM1EImLmTZCRqHrcu/aIFQuQKc7/+fasLs5v5ruEw6lMT5H2psDiimq5SR1HFYHYJ2iOxrE39SFIOTc7e/KZCx+jQlxb/1rJKMqJas4hklARfNqyMVfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=fYkv8Hx7FP5BupAvc4muf48bI8imuqjdwmR8u2171Q8=; b=A+VnnsFBba+ZieGb6PCb0zGzy6WvsoW3XvKfmpQ47Z7Peq/OsREQ3d7skxaiuXwbKfHWd7H9puKajLiuETCZgjE+tnwjut4gRh3yia5YsKewGE8sF5+R/sa3ti6Z3lig9W0Tp7y5zLt/n5OFdErY6VDM8NbnFb9QjFb6eKW/NPURQkLn+7n9znK86CooCnWE0WdURSuuBCwqy3/90hbNiQ1d+uQ7MKCzyRcTfyH8sCx6h+giixPihHgMlu8s+/xrg2zySmhyFmoasapPwtEi93rnUzOOifyaaAdb4A3KXdF1M2m+0w0m+qqg1fxa3Oe2PISLcs7oW4LHekHjDIx8/g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=prevas.dk; dmarc=pass action=none header.from=prevas.dk; dkim=pass header.d=prevas.dk; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=prevas.dk; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=fYkv8Hx7FP5BupAvc4muf48bI8imuqjdwmR8u2171Q8=; b=cmh6BdbAwJnvvHlHUPZyR62qC/20vQZzmRra834+CHzUQr/6CnJ82bAl2+wE7MRSPLoLEysQVh0hmC7vkCU7G2+XAPtWYsIYvl7FPLCNQDi3ZHL6S5zqpaSO/Y8wIbmZKql8Y2ubzfdB7sxydREFfoWJUqycHiCem/faB8hE10E= Authentication-Results: chromium.org; dkim=none (message not signed) header.d=none;chromium.org; dmarc=none action=none header.from=prevas.dk; Received: from AM0PR10MB1874.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:208:3f::10) by AM4PR1001MB1249.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:200:98::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4173.20; Thu, 27 May 2021 23:01:26 +0000 Received: from AM0PR10MB1874.EURPRD10.PROD.OUTLOOK.COM ([fe80::888d:190b:b3b5:1ad2]) by AM0PR10MB1874.EURPRD10.PROD.OUTLOOK.COM ([fe80::888d:190b:b3b5:1ad2%3]) with mapi id 15.20.4129.036; Thu, 27 May 2021 23:01:26 +0000 Subject: Re: [PATCH 1/5] lib/vsprintf.c: make sure vsnprintf() never returns a negative value To: Heinrich Schuchardt , Tom Rini Cc: U-Boot Mailing List , Simon Glass References: <20210520100528.322846-1-rasmus.villemoes@prevas.dk> <20210520100528.322846-2-rasmus.villemoes@prevas.dk> <6d1761b1-5129-d5a1-24ba-a27d15c42198@gmx.de> <20210521142726.GH17669@bill-the-cat> From: Rasmus Villemoes Message-ID: Date: Fri, 28 May 2021 01:01:24 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 In-Reply-To: Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [80.208.74.47] X-ClientProxiedBy: AM6PR10CA0045.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:209:80::22) To AM0PR10MB1874.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:208:3f::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from [192.168.1.149] (80.208.74.47) by AM6PR10CA0045.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:209:80::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4173.20 via Frontend Transport; Thu, 27 May 2021 23:01:25 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d228a2ff-e025-4f24-613f-08d921635acf X-MS-TrafficTypeDiagnostic: AM4PR1001MB1249: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2089; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Eqmb1fBFDkXqosq9NM+m69QqbpPc/Wi0gwvFDdcQM+nIcfXkkawght/7NFEXwdw2yRfzDszAjPNTiNK/WXbByhNJl1Hn+r0TWBku5doiAH/BIJsK/NE6sqjxR38/iweow+6D8LTMyEXkkPvfNCDgwH9nIptEDaA4wVLTC5GFgwsgohG4g4fimykz+jH7eHWINL6SbsEWX0UX1kAqGSeWnYGLgphr7bMiOHLf3oCMfLpMCIj2GXTe5CSzUbOGAxdbul+0Cu4tB7Jf0VUVu0WXmC8uODF+hgTNA62ry0LwZ+j5SRm6YXUtiZJzFBEzMhkWERWxSgXiGp/miIpNkkISZ/FCzzLQLQAoeWZHrhZs32nAFBeIRF9YOSIjIOfAKGhDxPhXgk7J7bGqr2tEBaKzVS8wmTBW56mvTLJdyLXsQm85mHSsaDDFfSUyflUfcd7VAVyCMaABUhfX/k0AvU9uy59zD3BGoBzIWqXdTe9KmMBPwJquLFlmaaTcuVdrbora1vF/JQ/ObifF21Fqw9kXhGio2WCPN+93EQ8j5oji0bkm39wUekLV6rthmKGeePotbih+B2OlvzRpCsoEba0K7xzFA0epEV74R6Ulez/tt24hk/N/ZBfSaSKTZKWbtQLO6Gtf9K9w12YmFWDNgw/Qo/X+0LxoNMRsPurLgIb+xpqZTy/Cb/pqXKjkCGBxIx3mQLObGMbKzYjXn4aZvs01HOWQoxCpFktx31rTtJYqkIg= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AM0PR10MB1874.EURPRD10.PROD.OUTLOOK.COM; PTR:; CAT:NONE; SFS:(39830400003)(396003)(376002)(136003)(346002)(366004)(36756003)(31686004)(66946007)(66476007)(4326008)(956004)(66556008)(53546011)(31696002)(38100700002)(83380400001)(38350700002)(6486002)(8976002)(2616005)(44832011)(8676002)(2906002)(54906003)(316002)(16526019)(186003)(110136005)(16576012)(52116002)(478600001)(86362001)(5660300002)(26005)(8936002)(43740500002)(45980500001); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData: =?Windows-1252?Q?LFIlj2CQ1L+t2CkXUJ8qQI1v5EHZ54XuElWJN1hjP3PcMelkhrNaUa/f?= =?Windows-1252?Q?Oxv+RUUscNDMwhPRKI0a2aD/rWH7ls3I35/bc5IR4UryNRqWt0HNjBd7?= =?Windows-1252?Q?uxAWSQvSjZr/QjFpMSdRZgIIJss6HLFVadRwlcGkUnPT8QeY4UGSm6hH?= =?Windows-1252?Q?tpr2cGtWNnlgT/N8pkS4mJtdko5OkG0N93qP0kt27DqVlopbH6m5ioak?= =?Windows-1252?Q?SdQ3Z4kznC2IK3NzE+0ozQV30uUL1UpMySr+/nlNvu4/5RgNTUukLrmA?= =?Windows-1252?Q?BpAMdVfNBilREapDDu+BWNwGen2aqUnteZsyWCk5ptkUgxttYaZ5alVm?= =?Windows-1252?Q?6ZcxYXyNxMQy7ClkLRYmmOYkRn0/EN3nGIJtt0qDk4uuVLIrxJQH5WtA?= =?Windows-1252?Q?4a6PuddzTBN9jzSzN7aEwi4KySC8snhP3yZZ+cqhLpzk0b44f/TNHqlU?= =?Windows-1252?Q?9opTcD2XPPAyxlY2zc96r4q5zNEtH3IfK8DFZReZ5FKM0Fk9BvgfJRFU?= =?Windows-1252?Q?L9CLrOTAlUO5rRWdxv9SwjwBbn0YMma0HHHaPg/rO8+FuCTyJrUqaKdC?= =?Windows-1252?Q?AZtYX2XhAFW0GMYmGRyVxzwMvbx44brr3whDUTX/YCH2cRYjhxzqYBUM?= =?Windows-1252?Q?KnPIXGTwv7bzKki00RwI5vWiW/RWSx2Rru0f25Mf9MRn0ToQxtqoX0go?= =?Windows-1252?Q?WQI8DxtKucTUaYL0PmFcujUn4KGVIbwwuqJyH4vZoA2Sv/h30pYlhkBZ?= =?Windows-1252?Q?j30PatcFiUz+O0SCHP9gklkOFs+DV2Yhy/esEfhqDPqSORkTEXFemm8c?= =?Windows-1252?Q?O1IUNoPbXsvK3GZiUdGbDSy7dPBANxovA2JJnDD1Q4G1OKOEzV3QSVer?= =?Windows-1252?Q?TNmWZCKkEsTR0WQj4MunA7l+++BCjeV/K+waUe+IpAsjv5P6eUJq5CV+?= =?Windows-1252?Q?yIetZ6ZNuB7KTSZeHSQcUOnt0BvCdtu+ruPeRoUXQIpMi/9xV1HUN8OJ?= =?Windows-1252?Q?ii6WjiaSfKuB8MWqXCOvE0SL0E+856D31Hpkcwirj+H7IfSmUb62R/lm?= =?Windows-1252?Q?NG7p291dG1baTj3J5ZmBX/jJN7g/8tpF2Maq0VxFrw4mAw8l3B1E92wm?= =?Windows-1252?Q?LVudFDV2ReWtpcQ5uDvyMkeJcYGt6YNdx85qMaaxTnMEN9C2GVktXNxD?= =?Windows-1252?Q?aZkQCX4FwsbMj4kSHqwUitO4fuF5kK6Y5dZQ2C1GjLhGzHQr/8v9sRaD?= =?Windows-1252?Q?Z9tsbbSOTuRRFlm87kucQ28TQkohHtqKdYUa06rrgueRDa+rovOO8bNw?= =?Windows-1252?Q?ROkyw9RvFP58TeXIQJkD+cNMWjCRqitq8JrkZliAk6t0fbzIwBd18NTe?= =?Windows-1252?Q?59LFMYY0SDKG8mtzb74WkE9F1klspzKK/VTfcJMAIqcQeJuP2FE5inFg?= X-OriginatorOrg: prevas.dk X-MS-Exchange-CrossTenant-Network-Message-Id: d228a2ff-e025-4f24-613f-08d921635acf X-MS-Exchange-CrossTenant-AuthSource: AM0PR10MB1874.EURPRD10.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2021 23:01:26.0164 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: d350cf71-778d-4780-88f5-071a4cb1ed61 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 094XRcqjMx7+iu3ZdDZDU2EELqSpSlgRIELrxZ1Qu4seABPVB7yPwTralgD8qTeccretTkjl7RYJj3G9NlbjzT+lV7nIQteyiCSUowRtnRc= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM4PR1001MB1249 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.4 at phobos.denx.de X-Virus-Status: Clean On 21/05/2021 16.42, Heinrich Schuchardt wrote: > On 21.05.21 16:27, Tom Rini wrote: >> On Fri, May 21, 2021 at 04:15:39PM +0200, Heinrich Schuchardt wrote: >>> On 21.05.21 14:53, Rasmus Villemoes wrote: >>>> On 20/05/2021 19.51, Simon Glass wrote: >>>>> Hi Rasmus, >>>>> >>>>> On Thu, 20 May 2021 at 04:05, Rasmus Villemoes >>>>> wrote: >>>>>> >>>>>> Most callers (or callers of callers, etc.) of vsnprintf() are not >>>>>> prepared for it to return a negative value. >>>>>> >>>>>> The only case where that can currently happen is %pD, and it's IMO >>>>>> more user-friendly to produce some output that clearly shows that some >>>>>> "impossible" thing happened instead of having the message completely >>>>>> ignored - or mishandled as for example log.c would currently do. >>>>>> >>>>>> Signed-off-by: Rasmus Villemoes >>>>>> --- >>>>>> lib/vsprintf.c | 10 +--------- >>>>>> 1 file changed, 1 insertion(+), 9 deletions(-) >>>>> >>>>> I think that is debatable. If we want the calling code to be fixed, >>>>> then it needs to get an error code back. Otherwise the error will be >>>>> apparent to the user but (perhaps) not ever debugged. >>>> >>>> But it is not the calling code that is at fault for the vsnprintf() >>>> implementation (1) being able to fail and (2) actually encountering an >>>> ENOMEM situation. There's _nothing_ the calling code can do about that. >>> >>> include/vsnprintf.h states: >>> >>> "This function follows C99 vsnprintf, but has some extensions:". >>> >>> The C99 spec says: >>> >>> "The vsnprintf function returns the number of characters that would have >>> been written had n been sufficiently large, not counting the >>> terminating null character, or a negative value if an encoding error >>> occurred." >>> >>> It is obvious that the calling code needs to be fixed if it cannot >>> handle negative return values. >>> >>> So NAK to the patch. >>> >>> Best regards >>> >>> Heinrich >>> >>>> >>>> The calling code can be said to be responsible for not passing NULL >>>> pointers, but that case is actually handled gracefully in various places >>>> in the printf code (both for %pD, but also plain %s). >>>> >>>>> The definition of printf() allows for the possibility of a negative >>>>> return value. >>>> >>>> First, please distinguish printf() from vsnprintf(). The former (in the >>>> normal userspace version) obviously can fail for the obvious EIO, ENOSPC >>>> reasons. The latter is indeed allowed to fail per the posix spec, but >>>> from a QoI perspective, I'd say it's much better to have a guarantee >>>> _for our particular implementation_ that it does not fail (meaning: >>>> returns a negative result). There's simply too many direct and indirect >>>> users of vsnprintf() that assume the result is non-negative; if we do >>>> not provide that guarantee, the alternative is to play a whack-a-mole >>>> game and add tons of error-checking code (adding bloat to the image), >>>> with almost never any good way to handle it. >>>> >>>> Take that log_info(" ... %pD") as an example. Suppose we "fix" log.c so >>>> that it ignores the message if vsnprintf (or vscnprintf, whatever) >>>> returns a negative result, just as print() currently does [which is the >>>> other thing that log_info could end up being handled by]. That means >>>> nothing gets printed on the console, and nobody gets told about the >>>> ENOMEM. In contrast, with this patch, we get >>>> >>>> Booting <%pD:ENOMEM> >>>> >>>> printed on the console, so at least _some_ part of the message gets out, >>>> and it's apparent that something odd happened. Of course, all of that is >>>> in the entirely unlikely sitation where the (efi) allocation would >>>> actually fail. >>>> >>>> If we don't want that <%pD:ENOMEM> thing, I'd still argue that we should >>>> ensure vsnprintf returns non-negative; e.g. by changing the "return >>>> PTR_ERR()" to a "goto out", i.e. simply stop the processing of the >>>> format string at the %pD which failed, but still go through the epilogue >>>> that ensures the resulting string becomes nul-terminated (another >>>> reasonable assumption made by tons of callers), and return how much got >>>> printed till then. >> >> So, how can we fix the callers without the above noted problems? >> > > The assumption that vsnprintf() is used to print to the console and that > writing some arbitrary string to the buffer is allowable is utterly wrong. > > vsnprintf_internal() is used to implement snprintf(). snprintf() is used > in numerous places where it will not lead to console output. > > Trying to solve one problem this patch creates a bunch of new ones. Heinrich, you do realize that the error handling you added in 256060e when you made it possible for vsnprintf() to return something negative is broken and incomplete? In multiple ways, even. First, let's look at vscnprint, which wasn't touched by 256060e. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) { int i; i = vsnprintf(buf, size, fmt, args); if (likely(i < size)) return i; Integer promotion says that, should i be -ENOMEM or some other random -Esomething, that comparison is false (for any realistic value of the size parameter), so we won't actually pass on that negative value. Instead, we'll fall through to the logic that handles "oh, vsnprintf() didn't have enough room, so the length of the generated string, which is what I'm supposed to return, is size-1". Hence printf(), which uses vscnprintf(), would in that case receive CONFIG_SYS_PBSIZE-1 as result, and then it would go on to puts() the printbuffer[] - which isn't a nul-terminated string because you did that early return, so it has stack garbage. Let us look at printf() in more detail. Assuming vscnprintf() forwarded a negative value directly, do you see the problem here: uint i; i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args); /* Handle error */ if (i <= 0) return i; So even if vscnprintf() was updated, printf() _still_ would go on to pass stack garbage to puts(). ==== Let me re-iterate what I believe any vsnprintf-implementation (and its close wrappers [v]s[c][n]printf) in a kernel or bootloader context must satisfy: - never return a negative value - given a non-zero size argument, must guarantee that the output buffer is a proper nul-terminated string. Without those guarantees, bugs like those described above will creep in, even if somebody fixes the issues I just pointed out. I'm not gonna send band-aid patches which will just propagate the problems to all other users. Not only do these guarantees make it easier to use the sprintf family, it also avoids lots of code bloat from unnecessary 'ret < 0' checks. It really has nothing to do with whether the output is destined for the console, _all_ users (direct or through several layers of helpers) of the sprintf family benefit from an implementation that provides these guarantees. Rasmus