From nobody Fri Nov 05 17:27:33 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 66F8518371D5 for ; Fri, 5 Nov 2021 17:27:47 +0000 (UTC) (envelope-from obiwac@gmail.com) Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) (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 4Hm6sG4fbFz4g6p for ; Fri, 5 Nov 2021 17:27:46 +0000 (UTC) (envelope-from obiwac@gmail.com) Received: by mail-ed1-x532.google.com with SMTP id f8so35725995edy.4 for ; Fri, 05 Nov 2021 10:27:46 -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=LxQls+Otm674yQv2LWE7RfHNPcwTQvUpy0RW4OQBueg=; b=mjxsmrFWpHoBcfH+xByZghy6teHWW+zHr9UOE7kG6R4l1PdBzx2IGU8HjLAdf3Ky37 908UZ364D+15FDXrTJYrgpnNRAPy8drJ/1AH3EtJmx95Q0EkMJF0lrH4XrVbLg1CJJy5 ZURSzQlOTqrF08w4hEwxlxTVQtIHQ0mXaux3zxVUkX0ZzzQclq9eKXJ4gZluzMhVg2SC 4F/vIWvjBKPbx2xZ0RIKRLIZ266JlttzBf7QTddPIHPRuGkDC4QajBbxUvZurNxp2FY+ xS87WoqRw9K64aFLvvRfbDVLK+CrQ/fvPRtDsOdBPGys0hM2x6e6u05kCBefyC5K/2Fr GAmQ== 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=LxQls+Otm674yQv2LWE7RfHNPcwTQvUpy0RW4OQBueg=; b=IgIs7R0Uh3XO1XZwfP3Qt3Fd2qoY7ayy8a1Ff8vVqLdIQEr3++YKJ5ec+nsVBSh3rb n+1ghMhw1UveL5joO7GCsuotYdsl3V3fVJPtQqUOyWs4yhNgmXqBSAyygIdVfVEpWah1 uhYFUhFusXZuJRoPs4GSEDWoaGhwT0bDfiAokcRZ/1vLBXJk68f7U+rfFuQInH5xZKmM GF42TwWd00V6XP8nohuy+qS9y5IUuZiIb1t1L9F7MUCV7cOsnskr2tRgd0U7qiQq0bVA RqveC1zmsCQr2Z7HPLY31LJ29yhu+hMZWZorg3YjhFkyaT6oscuHOc3duUqFyxKqXbGc QBfQ== X-Gm-Message-State: AOAM531Ld85IYqq7gx6bvzFksuCXJ1ddtC0wKp+mYuf2q3A0MwN2HCvJ 7cYOFG23vdQOFSZ+gk0LL/efw6nndeyXHFFeN6zI9I83Ogw= X-Google-Smtp-Source: ABdhPJzRlZbwJIu6I5h0HaZ1DmVUQqppHN9tb03/3FMfMbOkkH+DIm/0k85EbBNtKCKsBZtM9ZGMwbkY/AVUfRKF9H4= X-Received: by 2002:a05:6402:520b:: with SMTP id s11mr41716388edd.213.1636133264936; Fri, 05 Nov 2021 10:27:44 -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:27:33 +0100 Message-ID: Subject: Potential bug in the dynamic linker? To: freebsd-current@freebsd.org Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 4Hm6sG4fbFz4g6p X-Spamd-Bar: / Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20210112 header.b=mjxsmrFW; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mx1.freebsd.org: domain of obiwac@gmail.com designates 2a00:1450:4864:20::532 as permitted sender) smtp.mailfrom=obiwac@gmail.com X-Spamd-Result: default: False [-0.99 / 15.00]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-0.997]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20210112]; FROM_HAS_DN(0.00)[]; DWL_DNSWL_NONE(0.00)[gmail.com:dkim]; R_SPF_ALLOW(-0.20)[+ip6:2a00:1450:4000::/36:c]; FREEMAIL_FROM(0.00)[gmail.com]; MIME_GOOD(-0.10)[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.992]; MID_RHS_MATCH_FROMTLD(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DKIM_TRACE(0.00)[gmail.com:+]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; RCVD_IN_DNSWL_NONE(0.00)[2a00:1450:4864:20::532:from]; NEURAL_SPAM_SHORT(1.00)[1.000]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:2a00:1450::/32, country:US]; SUBJECT_ENDS_QUESTION(1.00)[]; RCVD_TLS_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2] X-ThisMailContainsUnwantedMimeParts: N 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?