Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2145C433F5 for ; Tue, 12 Oct 2021 12:45:16 +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 DA50361039 for ; Tue, 12 Oct 2021 12:45:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org DA50361039 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=prevas.dk Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A0DBB83740; Tue, 12 Oct 2021 14:45:13 +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="Uxc3LKJV"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D64518374A; Tue, 12 Oct 2021 14:45:10 +0200 (CEST) Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05on2071d.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e1a::71d]) (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 EC67F8372F for ; Tue, 12 Oct 2021 14:45:06 +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=VfNpi+DxHNlXonQPArtvlKQvwyqVM+joFovWEeNmXFqBgKP+sKCgMGWerl60SGVJZSvPj1cKX7thEPXAX/tYTrBHFWDubp2CoJSeJxhQZ1B7sO9ktWX+h63bMoTR7JuUs1fZeDdbLYJEkY+xXPF7zrfIF5olFlvrD7tW7i3xCBSVgJi5COa8x1LZKv99H+L16FQYfBfwZczhcpNFv5jW+vlszntl2IWjOliLON9VfhHICqRPkU1SYgan35D7vaNSFZfP8gTWesmvy54PKYNUEn1Qr687UJ2q/nyyDpXTJWv0drSmc0+OTMa8gvU9Evt95AorENqFtlKmEif8D361xQ== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=fIt9+TZIVeHw9J0I7nGhZZ5/FDGw0cOOiRXh8s27laU=; b=TI47pHyW9ppMWdhbK1qVW0PP2t4D9wjyF7H6AdU7XYglKnkuAHI8dIKb8iIT+FPki/nBVX0UbrJLeOkrO3ULAIWkl+l5TtWq5ULkBJXfOIE3ujomkCFzwV3zkxrAuD72GYGUtUb5hPWqMhsGmHrWQb1JX6RIZy17ibkErHtFlsZGL8E3t9urpNEYWY0n7sNbH4B8oC6a7v1HD1lznoDlKUiL3via9yq8Kplw5VW4FnnWNChzGTotAxwqXs1iDMDjEnBkoJc318SvUstxSwsMtwqphRGOB/P/eZHH72U5mLRx9+UxgpB9gqH9r04GhlbAu8J5pUHxQESicTOsXd/YNQ== 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=fIt9+TZIVeHw9J0I7nGhZZ5/FDGw0cOOiRXh8s27laU=; b=Uxc3LKJVTRcf8PInn8Bv/tl/ew30KeqUk6cfsqzNgxrLnXt7oOaF+4zQGdsQIQjBKxsSUV7tvqSw3Z+M8OcQ1gHSi91JDxI3kyvWM/azqa+EcecxCHfxuG0hb1wT6QWvqmqaODfdYwSW3jaD3t6A0uiIRZxJB2Cu86+32sagQQ0= Authentication-Results: nic.cz; dkim=none (message not signed) header.d=none;nic.cz; dmarc=none action=none header.from=prevas.dk; Received: from HE1PR1001MB1355.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:3:f5::16) by HE1PR1001MB1065.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:3:6b::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4587.20; Tue, 12 Oct 2021 12:45:05 +0000 Received: from HE1PR1001MB1355.EURPRD10.PROD.OUTLOOK.COM ([fe80::1574:9b9b:be30:c18d]) by HE1PR1001MB1355.EURPRD10.PROD.OUTLOOK.COM ([fe80::1574:9b9b:be30:c18d%12]) with mapi id 15.20.4587.026; Tue, 12 Oct 2021 12:45:04 +0000 Subject: Re: [PATCH 08/10] env: Use strncpy() instead of ad-hoc code to copy variable value To: =?UTF-8?Q?Marek_Beh=c3=ban?= Cc: Simon Glass , Tom Rini , U-Boot Mailing List , =?UTF-8?Q?Marek_Beh=c3=ban?= References: <20211012110501.6118-1-kabel@kernel.org> <20211012110501.6118-9-kabel@kernel.org> <20211012140018.3a4502e6@dellmb> From: Rasmus Villemoes Message-ID: <52bc92d4-8651-48df-3577-043aa2e16722@prevas.dk> Date: Tue, 12 Oct 2021 14:45:01 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 In-Reply-To: <20211012140018.3a4502e6@dellmb> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-ClientProxiedBy: HE1PR09CA0047.eurprd09.prod.outlook.com (2603:10a6:7:3c::15) To HE1PR1001MB1355.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:3:f5::16) MIME-Version: 1.0 Received: from [172.16.11.1] (81.216.59.226) by HE1PR09CA0047.eurprd09.prod.outlook.com (2603:10a6:7:3c::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.14 via Frontend Transport; Tue, 12 Oct 2021 12:45:04 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 92d66ebe-3ef7-466e-1d17-08d98d7e1d2f X-MS-TrafficTypeDiagnostic: HE1PR1001MB1065: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: AI2KElAWni00RfDuGaXG6+m41J6otMg54c4GGqs29tKhIFJSNYuN9a+7+HqQdc1sfecKYua7FDX2fIMRyFynj30jTHgxZSGKqKdKfRh4CweWk0TvWK9CoxBZzO47/6WR8PK2oCirJE/sOy1OINKcZ+C0DdZW/rRRcPJXaCmu28vxlS4p8mK/hM+SJ2HRzghVpBBX0BcMM+1XgcIgvyaHNC4nJbB7YTVpKAQHkPY5+CmBX1sDy1NtCY8cpiFx+/NuzeaHq2s89Dz4l7Nyf3gOHoB6VL3IcM5mLAgY1E0Odxbjn5RcX71Z3uqlML689XKzjalpVLFeGdaWifx3VlnG3s4NMTE7nbga0m8b4dtne7noSBUw6VtxMY9EQAd7OAIy8psqssbfEMyE8tfLdGydL0zpI9sU77mPKXOmo1BXsO0C1xpQE+umC0MgMqbf/zhc2bUvzOT2wtG/9aq755wW3P1iEKV0aX48lbVNdBf4Zp7zN+LKeIddpD1/2TIkqg+smiQYwLIuWAeNAyNskupuku7m/jDd0MosoIVg7BmgFgpUoMEhqleKVq2VYa7/s7ECpVzZflhbInaDZvOm6JfMxFTwhNj0p/N4b/sYTyDk3sIYtp3C7w2nyPjMq+uGKnJWDB2mSeD6phNAtyjS7AOX9PaC7Pi3RAHAC02LGE02uXJsBT6YdYpm1TlWCnnKu//bz9KOuX1tZeqtECUbwRv1BoHbxtGfHv1biOhQ3vmTfKTTklCCwVTw1JTqoEiLkbHGzY+fHPYEPb/bRAp5zZIAJHXKHlEAW6k8yg9kpvOahh7z/EWBYro6Q4pCzUs9WRdi1FvuBXKmXQZR9+h2d9JW9XW94p0GrDpAgz7foFW+GbM= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:HE1PR1001MB1355.EURPRD10.PROD.OUTLOOK.COM; PTR:; CAT:NONE; SFS:(366004)(2616005)(6666004)(26005)(8936002)(44832011)(66556008)(66476007)(83380400001)(6486002)(186003)(52116002)(66946007)(956004)(8976002)(38100700002)(31686004)(38350700002)(54906003)(8676002)(316002)(31696002)(16576012)(4326008)(2906002)(36756003)(5660300002)(508600001)(6916009)(966005)(86362001)(45980500001)(43740500002); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?WXdYNXVwb29zK1lFKytzRU83YTM2WmlOaEM5UlRwZ0JmUlFOaGgydS9Oaldw?= =?utf-8?B?ZUhFSzBaK3g3cmFOYnZ4bW1vTkJqOTRkUFFJVWllUVRBVnhkdlg5dmdJWTZT?= =?utf-8?B?b1ovVmlRTXMwNlBvaGwvSXpqMllWZlRUVktEMGExSjAyb1FDWUY2TVZJWVd6?= =?utf-8?B?T3l5emlxWnNnSGVZNURMSk9mOVBteEtVcUU4MUhIS1djZXBObzQ1T29oSnhR?=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UZXlEcjRDUmZ2?= =?utf-8?B?Kyt6b2FkaUw4K0I1REp1K0dxQk5Cc0lhWkhhK011dnA3aUdROXJicnNreHpi?= =?utf-8?B?L1pGNG5pb0JyL2NuZGppUWIvaUJ1ZTB6Z2xDdWVmZ0ttUVFubUpTWGdDVmZI?= =?utf-8?B?MHF4ZDluZXFUMFdEYXRqdUhjNVNGMDgwTHdBMWtHMld5WUZBL0xlRDl0dDhS?= =?utf-8?B?dDh3NE4yckduakFZNUYzRDhKNFNKN2IxVjNkdlo4eDdnTC9yNXJYWkdXRG02?= =?utf-8?B?OHBMd0JFalpKdTFXUXExUXcyL1dpa251R2JYbTY5bURxeDVVMW9XY28vdGw4?= =?utf-8?B?OFFSRDE0aGozcUt3a280NXFZV1JSMkRPZ05QODJMalJkUG56V3ZXcjBWQU8x?= =?utf-8?B?VnYwT3Myc1VJWURBK2RqRlVJYy9pY3hMNXJlTXlZcUFkZTRWR2NXa0dZUVhP?= =?utf-8?B?dkRqTWhoZ281MElCUzNCMEo4NEJ4ZFdtZUxZYjhPejFmSFdWd0ovRjVoK2RJ?= =?utf-8?B?cGN5QkpYY2lCM2tzOEhTVDdHaUpKTGd1UFV1V2NYOVpPaXA0WmVobWZwQVVq?= =?utf-8?B?aTdYZmo0cXZYUzdlOHNXMlJxY1FKRGNSN0ZuSkhWY1B1dnRKRlV5L1ppVUF4?= =?utf-8?Q?1kdAyChSkP4WYyYsAF1uI/uIR9iMPXaSjeOAwFl?= X-OriginatorOrg: prevas.dk X-MS-Exchange-CrossTenant-Network-Message-Id: 92d66ebe-3ef7-466e-1d17-08d98d7e1d2f X-MS-Exchange-CrossTenant-AuthSource: HE1PR1001MB1355.EURPRD10.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Oct 2021 12:45:04.8666 (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: I6I+d4r8vzBSQjEEWpaX58YHWfjwZSxlsZOw15BuFl3lgWw/JYUoT0NKrsUFvtE0/pqOEW0Pu1du8gzByqJSZ2e5YzP5jAcTPOfECIRfwoU= X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR1001MB1065 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.103.2 at phobos.denx.de X-Virus-Status: Clean On 12/10/2021 14.00, Marek BehĂșn wrote: > On Tue, 12 Oct 2021 13:41:43 +0200 > Rasmus Villemoes wrote: > >> On 12/10/2021 13.04, Marek BehĂșn wrote: >> I understand why you want to avoid an open-coded copying, but strncpy >> is almost certainly the wrong tool for the job. Apart from not >> revealing anything about the actual length of the source string, it >> also has the well-known defect of zeroing the tail of the buffer in >> the (usual) case where the source is shorter than the buffer. > > U-Boot's strncpy does not do that: > * Note that unlike userspace strncpy, this does not %NUL-pad the > buffer. > * However, the result is not %NUL-terminated if the source exceeds > * @count bytes. Yeah, I just saw that. That's extremely dangerous, and can cause silent wrong code generation. The compiler knows the semantics of various standard functions, including strncpy, and can optimize accordingly. For example, look at https://godbolt.org/z/855s7hsEE In g1, we see that gcc has recognized strncpy() and open-codes it by two immediate stores (6384738 == 0x616c62 == ascii "bla"), including the trailing zero padding. Then in g2, gcc further realizes that the conditional is always false, hence folds the whole thing to "return -1" (though it does not manage to elide the then-dead stores to buf). So, I wouldn't bet that in cases where the compiler _does_ end up emitting a call to strncpy() that it doesn't somehow also rely on the implementation actually honouring the semantics required by the C standard. For another example, see https://godbolt.org/z/W6W3od The only way it is ok for gcc to compile copy_and_do to the exact same machine code as copy_and_do2 is because it knows memcpy returns its dst argument, hence (in the x86-64 case) it can prepare the first argument to do_stuff via a "mov rdi, rax", instead of spilling and reloading p. Well, U-Boot seems to pessimize the build with -fno-builtin making much of the above moot. But: On pa-risc, until very recently, "prctl(PR_SET_NAME, "x"); ....; prctl(PR_GET_NAME)" was a simple way to read 14 bytes of kernel stack from userspace because pa-risc's strncpy didn't do that zeroing. Of course U-Boot doesn't really have problems with that kind of info-leak, but given the amount of code U-Boot imports from other projects, not least linux, there's certainly a fair chance that some of that code relies on strncpy's semantics for correctness. Rasmus