From owner-svn-src-all@freebsd.org Wed Nov 18 22:15:02 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id A3124473AB6 for ; Wed, 18 Nov 2020 22:15:02 +0000 (UTC) (envelope-from jrtc27@jrtc27.com) Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4CbxvB4333z4kx4 for ; Wed, 18 Nov 2020 22:15:02 +0000 (UTC) (envelope-from jrtc27@jrtc27.com) Received: by mail-wm1-f49.google.com with SMTP id a3so4384805wmb.5 for ; Wed, 18 Nov 2020 14:15:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=oOQYEIyoPLRaenKexu+jdMHwK667HraHGyVAaSsV7mI=; b=D9V0AqYh37Feaa9NFPwjjhysr0W68SGGw7Tj6hh3FQNhFReLV3zfs3/7PXhPEMMmTy VlUD+3729rOTL4zm2Q2S6xsBh7m19kZRpODjb8rehq7QoxZvXX1IGaoaHE6RPXV7YECB HRhHTK3uFt9PscQ4NNjO2kVUxqAksSDNIy5tnappRtIsnFqszYKqPvUuUz6qJLCF0w6b GanD28xE7P3NWFP4x7tHeu8hF8v9j1qug8TylrDHOE9ltfeSqIoTiVkkd2zznR9mIkUV q23Iy1AVXmZuQ+8lWSQgmWElTsU4syPE5gqKc3Hqi7ebmCT8IQ+I0icjwD35kGO2VmRl MsOA== X-Gm-Message-State: AOAM533tbypG479gVV5p6jJSMBj+tU1W/kWJ0bwhCU2QFnAlDf+1NJTS 2dTsFp3ETOnWhCa3JJynw58pFg== X-Google-Smtp-Source: ABdhPJzULJAYFEyVtXEC1XKbKOo0CWKOJlostwvInbeQriFxcyy1swfd2sY1BF6X5vFbqWrlT7jNlg== X-Received: by 2002:a1c:27c2:: with SMTP id n185mr1261356wmn.102.1605737701073; Wed, 18 Nov 2020 14:15:01 -0800 (PST) Received: from [192.168.149.251] (trinity-students-nat.trin.cam.ac.uk. [131.111.193.104]) by smtp.gmail.com with ESMTPSA id f5sm36626242wrg.32.2020.11.18.14.15.00 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Nov 2020 14:15:00 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) Subject: Re: svn commit: r367813 - head/lib/libutil From: Jessica Clarke In-Reply-To: <92a87ed8-4b22-4d03-9481-b02f92dcaaa0@freebsd.org> Date: Wed, 18 Nov 2020 22:14:59 +0000 Cc: src-committers , svn-src-all , svn-src-head@freebsd.org Content-Transfer-Encoding: quoted-printable Message-Id: References: <202011181944.0AIJiUU3003699@repo.freebsd.org> <25465269-5497-4981-A1E4-CC1FFAB68CF4@freebsd.org> <92a87ed8-4b22-4d03-9481-b02f92dcaaa0@freebsd.org> To: Stefan Esser X-Mailer: Apple Mail (2.3608.120.23.2.4) X-Rspamd-Queue-Id: 4CbxvB4333z4kx4 X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[] X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 Nov 2020 22:15:02 -0000 On 18 Nov 2020, at 21:52, Stefan Esser wrote: > Am 18.11.20 um 22:15 schrieb Jessica Clarke: >> On 18 Nov 2020, at 19:44, Stefan E=C3=9Fer wrote: >>> + /* >>> + * Check for some other thread already having >>> + * set localbase - this should use atomic ops. >>> + * The amount of memory allocated above may leak, >>> + * if a parallel update in another thread is not >>> + * detected and the non-NULL pointer is overwritten. >>> + */ >> Why was this committed with a known racy/leaky implementation? >=20 > Because the alternatives that I offered for discussion were > less acceptable. That has no bearing over whether this one is. >> What happens if I set the value with a sysctl and call this? >=20 > You'll get the value set with sysctl, unless overridden by the > environment variable. There is a window of a few nano-seconds > where a thread executing in parallel on another core might be > able to set the localbase variable (between the test for NULL > in this function and the assignment to it). The value that will > be returned by either thread will be identical, so there is no > risk of corruption of the result. But if I call getlocalbase, then set it via sysctl, then call getlocalbase again, I see the old value. If, however, I omit the first getlocalbase, I see the new value. This differs from how getenv/setenv of the value work, where you always see the up-to-date value. Maybe you think that's a feature, but it's something to watch out for and explicitly call out in the documentation. You also misunderstand all the subtleties of multithreading here. There are no acquire/release pairs so it is entirely legal for Thread 2 to read Thread 1's initialised value for localbase before the contents of it are visible (i.e. the pointer is initialised but the data is garbage). The `(volatile const char*)localbase` cast is also a complete waste of time. You probably meant to write `(const char * volatile)localbase` but even then that does nothing useful as the cast is too late. What you really were trying to write was `*(const char * volatile *)&localbase`, but you need proper atomics anyway for this to be safe. > This unlikely case may actually leak a heap allocated string > of typically tens of bytes (but with negligible probability). >=20 > But this really is a non-issue, since there should never be a > reason to invoke this function in a multi-threaded context. Why not? There could easily be code out there calling getenv in a multi-threaded context so this is inadequate as a replacement. Yes it's inefficient but it's perfectly legal and imaginable. Also if malloc returns NULL I would quite like that to be an error for the function and not silently fall back on _PATH_LOCALBASE. > The result should be constant for the duration of execution > of the process (expect severe inconsistencies if that was not > the case) and all programs in base that are candidates for the > use of this function are non-threaded (and if they were multi- > threaded, then I'd expect this call to occur during start-up > of the program before any further threads are created). >=20 > So, this is a non-issue and the comment tries to explain it. > Did I fail to make this clear in the comment? Maybe I should > have written "could use atomic ops" instead? >=20 > Use of atomics or locks could prevent the race-condition. But > since I do not expect this function to be called from within > threads (it just doesn't make sense), the tiny time window of > a few nano-seconds which might lead to a double assignment to > the target variable (with one pointer value being lost), and > the worst case loss of 1KB of heap space in that case (more > likely 10 to 20 bytes rounded up to a 16 or 32 byte chunk), I > do not consider the complexities of either a lock or atomic ops > to be justified. >=20 > Regards, STefan >=20