From nobody Fri Nov 05 17:25:19 2021 X-Original-To: freebsd-current@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id C8CEC183535A for ; Fri, 5 Nov 2021 17:25:33 +0000 (UTC) (envelope-from obiwac@gmail.com) Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) (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 1D4" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Hm6ph4dd1z4dCn for ; Fri, 5 Nov 2021 17:25:32 +0000 (UTC) (envelope-from obiwac@gmail.com) Received: by mail-ed1-x529.google.com with SMTP id m14so34791349edd.0 for ; Fri, 05 Nov 2021 10:25:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:from:date:message-id:subject:to; bh=7WpwGQ1P1cxUxNxLqaNHIWJ5zo9Tf76MlpU+sAjxPfs=; b=czLnsahQ3YCf9S4122rBt5i/MnVUlPRLvRKEHG7mHXEfq/YJSFWCtE+f0D0t1hBekH hacOTDakOgymcmMDMPKYxk+Rr2ycpzPhfUkZAN7DtknVerIwTXiHu2Jd3aXuKdp+b1Da BlYvhkaePovxXpJH7bvZwREuN0ZN+/3MLXA4sRn/4kwipqm6Jk2Mi5MoCJlipkuv5W+R 6XVk7mk21ezZzRwXaVU9W6HO+MDBXJVzNKOqGrSaHd3jCtZsANBbhW/01d553tBxLwRw bxv0apKMNebXK+eeQco7+mKUxAnwUOzE3szX+zrDQA6S+pGjYoYXFkKLWGscGtaScPpi EY0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=7WpwGQ1P1cxUxNxLqaNHIWJ5zo9Tf76MlpU+sAjxPfs=; b=71U6A/BFuzcTcir37GHlr25zTo/7NWcqxpraNeCo14qRE2eq4zpyZY6x+ZeeNhe835 DggZhlSfr7DWfZyl94c8vA0cp0gWRYx5KgN8XqBJtMyahRZDhJ8i2ENvvfOk0NXwClDI I+rnGKNultI/hNzguqCjU21avp+BoCfxhGEn+KPvZmvYv+X1swWobe1eVc8WCXntxj7q uB4qSHkliROmHoqJfss2LKUjfldrOjlHGchdvJbdo8S1Rw7tge/K0Yt4PCIGnJW5Qa0I KNx89E/aDDx0zbS+1RIktgjUCIs9+7A2+IEFtBsdA5RQAC3hw+ZfFkDrsxa1MGoVHYw3 1iaw== X-Gm-Message-State: AOAM5307w7mkUmS0CPMLpGr4Hmt3PUFXaKLKRTt41Pwlc20/I0OnL+V7 JZHtcApUFgG/cdixhgSZV56RNluM2qBYxqwkqjlm1PMVaOI= X-Google-Smtp-Source: ABdhPJwbFu8f0Pg9OtsndIKalWU0Bq8xZTA6X/vfY6xqj7rSi5ArCnBaNldYP2PUW+gI6s1Mpx/81CR9q8oFXzrbFuI= X-Received: by 2002:a17:907:6da4:: with SMTP id sb36mr5007760ejc.40.1636133130797; Fri, 05 Nov 2021 10:25:30 -0700 (PDT) List-Id: Discussions about the use of FreeBSD-current List-Archive: https://lists.freebsd.org/archives/freebsd-current List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-current@freebsd.org MIME-Version: 1.0 From: obiwac Date: Fri, 5 Nov 2021 18:25:19 +0100 Message-ID: Subject: Potential bug in the dynamic linker? To: freebsd-current@freebsd.org Content-Type: multipart/alternative; boundary="00000000000011101005d00dee84" X-Rspamd-Queue-Id: 4Hm6ph4dd1z4dCn X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20210112 header.b=czLnsahQ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mx1.freebsd.org: domain of obiwac@gmail.com designates 2a00:1450:4864:20::529 as permitted sender) smtp.mailfrom=obiwac@gmail.com X-Spamd-Result: default: False [-2.99 / 15.00]; SUBJECT_ENDS_QUESTION(1.00)[]; ARC_NA(0.00)[]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20210112]; NEURAL_HAM_MEDIUM(-1.00)[-0.997]; FROM_HAS_DN(0.00)[]; FREEMAIL_FROM(0.00)[gmail.com]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[multipart/alternative,text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-current@freebsd.org]; TO_DN_NONE(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; NEURAL_HAM_LONG(-0.99)[-0.991]; MID_RHS_MATCH_FROMTLD(0.00)[]; R_SPF_ALLOW(-0.20)[+ip6:2a00:1450:4000::/36]; DKIM_TRACE(0.00)[gmail.com:+]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; RCVD_IN_DNSWL_NONE(0.00)[2a00:1450:4864:20::529:from]; NEURAL_HAM_SHORT(-1.00)[-1.000]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+,1:+,2:~]; FREEMAIL_ENVFROM(0.00)[gmail.com]; RCVD_COUNT_TWO(0.00)[2]; ASN(0.00)[asn:15169, ipnet:2a00:1450::/32, country:US]; RCVD_TLS_ALL(0.00)[]; DWL_DNSWL_NONE(0.00)[gmail.com:dkim] X-ThisMailContainsUnwantedMimeParts: Y --00000000000011101005d00dee84 Content-Type: text/plain; charset="UTF-8" Let me preface this by saying that I am in no way knowledgeable enough regarding the FreeBSD dynamic linker to know whether or not this is infact a bug or intended behaviour. This program I'm working on, when compiled for FreeBSD, calls fdlopen(3) to load a dynamic library from memory. This is how I'm doing that more specifically: // void* lib_bin, size_t lib_len int fd = shm_open(SHM_ANON, O_RDWR, 0); ftruncate(fd, lib_len); void* lib_mem = mmap(NULL, lib_len, PROT_WRITE, MAP_SHARED, fd, 0); memcpy(lib_mem, lib_bin, lib_len); munmap(lib_mem, lib_len); void* lib = fdlopen(fd, RTLD_LAZY); close(fd); Running this on FreeBSD 13 works fine, FreeBSD 14, however, spits out this error: Cannot fstatfs "" Digging around, I find, in libexec/rtld-elf/rtld.c: /* * but first, make sure that environment variables haven't been * used to circumvent the noexec flag on a filesystem. */ if (dangerous_ld_env) { if (fstatfs(fd, &fs) != 0) { _rtld_error("Cannot fstatfs \"%s\"", printable_path(path)); return NULL; } if (fs.f_flags & MNT_NOEXEC) { _rtld_error("Cannot execute objects on %s", fs.f_mntonname); return NULL; } } And this is the first thing that seems weird to me. Why is it calling fstatfs(3) before checking if the file descriptor doesn't necessarily refer to a file which resides on a physical filesystem? It doesn't say so on the manpage, but, again, digging around, that's what the error returned by fstatfs(3), EINVAL, supposedly means. Secondly, why then is dangerous_ld_env even set in the first place? Well, as of this commit (https://reviews.freebsd.org/D26352): ld_dynamic_weak = ld_get_env_var(LD_DYNAMIC_WEAK) == NULL; ... dangerous_ld_env = libmap_disable || libmap_override != NULL || ld_library_path != NULL || ld_preload != NULL || ld_elf_hints_path != NULL || ld_loadfltr || ld_dynamic_weak; Should this not be ld_dynamic_weak = ld_get_env_var(LD_DYNAMIC_WEAK) != NULL; instead? Or is this actually intended and am I just not understanding the point of this? --00000000000011101005d00dee84--