From nobody Fri May 29 16:01:38 2026 X-Original-To: dev-commits-src-main@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 4gRp4z1lX1z6flVG for ; Fri, 29 May 2026 16:01:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R13" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4gRp4y6cyXz3HdH for ; Fri, 29 May 2026 16:01:38 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1780070498; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=kFcb9ZEc3mjBdzo3YHx2YQvrAIosGUc0ssCmoUOw6AM=; b=gMJAH0f5eTBTjlQ949PeRe+1jA0gqiBmeto6nM0KiIOE8/S3RjP+/KmEmQbnQEJ1+nx5QV At3t08QZhZfjqk08jAE3n6VNrQ+QVoWCBTNu5c7BzACVMrfXZZMLDrI7i1x3vIXl2CyDF0 ADsx9OweMAePYviMaDzsMIRzojkSAfck3M5J9whFuWqbiOAO4P8cQokC0bu2MOeNjoL8Nz ay0zN8XYOH+DZ6TtzHPollOs/rf1T3Nda5eCh9IBIxxEM0qqtXRLgyWLRjbAcQoE3655gB 06qYqV22dtdNKoANqICrBnOulP0t0QgBH9l5dXMDQxEoXgTgNicBbhvw6G1ccg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1780070498; a=rsa-sha256; cv=none; b=QxV3G40iLVXtoDVMvK/qqmTjChgXaWdvBhhMnUL8C4PJJuBR8V37DKJ3Y48bcH7MWk7RES R40tdp1xAyTCguYLHP6La2pGoNg5XgkL9roeeJ9uf+M5880LCGAKXDoeuJEoyATgvjB4GY 6/Bt/tLzM/eoNfYK8g8z09eExYAtPlxDxzn3wqxB7+bfFt7S+RvN94nUnzW3GEIwDcMr/2 j6YXdEgJ9Ij0RnXnXIGicNiRfELw5B3JN4VNljVxI4fbtvfK0gJL/gwphaReEYc7vkdg8a 8dW/NqsvVov9w/bbNHZYLYi1DNh84a/6RH0QbjPf2NjkCnXH3XB7DdVohsvU1g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1780070498; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=kFcb9ZEc3mjBdzo3YHx2YQvrAIosGUc0ssCmoUOw6AM=; b=gCd8rdecYYoGjxBnAntAlb81bt5HJlmcqG3vLnPC/9TlDIx0Fv+GlzEmIkEPBGSYtK+cWn GvMkbu5B7CwvHfQJXWp5rEcyUhwDwGjZ8PG0PGADIy4cw/5iNbn43LksuSoIewqmN7rWyH BOhmozRolnergOYsoiXdnA7WQKwZcy2xtGnX6Xi+kRtMpPEQQi6JWOXDQe007Q0fUT7LuR GhpQPLMTPDZXW0L5PADBWKeajgbMXgeWaEi3BakXpewZVaE1jX99yEJ9dHmyorGWOMfBtI 2iNhOxWDRlV8mMATFWKAmV2hqdZesSUisc6TSHb50ISYx7WVy6DtiMa3bCXaiw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4gRp4y6Dt2zg6X for ; Fri, 29 May 2026 16:01:38 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 3601a by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Fri, 29 May 2026 16:01:38 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Olivier Certner Subject: git: a7a9e6cc64aa - main - MAC/do: Fix releasing a nonexistent reference on configuration parsing error List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: olce X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: a7a9e6cc64aa90a899aad9ea1395bfc77bb26f48 Auto-Submitted: auto-generated Date: Fri, 29 May 2026 16:01:38 +0000 Message-Id: <6a19b862.3601a.7ce95a22@gitrepo.freebsd.org> The branch main has been updated by olce: URL: https://cgit.FreeBSD.org/src/commit/?id=a7a9e6cc64aa90a899aad9ea1395bfc77bb26f48 commit a7a9e6cc64aa90a899aad9ea1395bfc77bb26f48 Author: Olivier Certner AuthorDate: 2026-04-28 08:42:01 +0000 Commit: Olivier Certner CommitDate: 2026-05-29 15:22:21 +0000 MAC/do: Fix releasing a nonexistent reference on configuration parsing error On parsing error, parse_and_set_conf(), introduced with the recent "executable paths" feature, has been calling drop_conf() on the being-built configuration. However, that configuration structure is allocated through alloc_conf(), which does not grab a reference. Calling drop_conf() on it, which releases a reference, is thus erroneous, and causes the underlying counter to saturate, translating into a memory leak. To fix this bug, make alloc_conf() grab a reference on the newly-created 'struct conf', and rename it to new_conf() to be more in line with what it does. Keep set_conf() as is, i.e., grabbing an additional reference on behalf of the jail that is going to hold the configuration. Consequently, make sure that callers of alloc_conf() unconditionally drop the reference acquired by the latter before returning (i.e., even if set_conf() has been called). While here, since hold_conf() is always used to obtain additional references on a configuration (new_conf() does not use it, instead directly setting the use count), add an assertion that it is never used on a configuration that has no references at all (which indicates that the configuration has been destroyed). These changes generally simplify the lifecycle of configurations, reducing the probability of re-introducing reference mismatches (at the expense of slightly more reference counting operations, but performance does not matter here). Reviewed by: bapt Fixes: 9818224174c4 ("MAC/do: Executable paths feature (GSoC 2025's final state)") MFC after: 1 month Sponsored by: The FreeBSD Foundation Pull Request: https://ron-dev.freebsd.org/FreeBSD/src/pulls/38 --- sys/security/mac_do/mac_do.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/sys/security/mac_do/mac_do.c b/sys/security/mac_do/mac_do.c index a2259ed777ae..58abf1a8f608 100644 --- a/sys/security/mac_do/mac_do.c +++ b/sys/security/mac_do/mac_do.c @@ -366,14 +366,14 @@ init_exec_paths(struct exec_paths *const exec_paths) } static struct conf * -alloc_conf(void) +new_conf(void) { struct conf *const conf = malloc(sizeof(*conf), M_MAC_DO, M_WAITOK | M_ZERO); init_rules(&conf->rules); init_exec_paths(&conf->exec_paths); - conf->use_count = 0; + refcount_init(&conf->use_count, 1); return (conf); } @@ -1163,7 +1163,10 @@ error: static void hold_conf(struct conf *const conf) { - refcount_acquire(&conf->use_count); + int old_count __diagused = refcount_acquire(&conf->use_count); + + KASSERT(old_count != 0, + ("MAC/do: Trying to resurrect a destroyed configuration.")); } static void @@ -1329,7 +1332,7 @@ set_conf(struct prison *const pr, struct conf *const conf) static void set_default_conf(struct prison *const pr) { - struct conf *const conf = alloc_conf(); + struct conf *const conf = new_conf(); strlcpy(conf->exec_paths.exec_paths_str, "/usr/bin/mdo", MAX_EXEC_PATHS_SIZE); @@ -1337,6 +1340,7 @@ set_default_conf(struct prison *const pr) conf->exec_paths.exec_path_count = 1; set_conf(pr, conf); + drop_conf(conf); } /* @@ -1406,7 +1410,7 @@ parse_and_set_conf(struct prison *pr, const char *rules_string, if (need_applicable_conf) applicable_conf = find_conf(pr, NULL); - conf = alloc_conf(); + conf = new_conf(); if (rules_string != NULL && rules_string[0] != '\0') { error = parse_rules(rules_string, &conf->rules, parse_error); @@ -1429,12 +1433,12 @@ parse_and_set_conf(struct prison *pr, const char *rules_string, MPASS(error == 0 && *parse_error == NULL); out: + drop_conf(conf); if (applicable_conf != NULL) drop_conf(applicable_conf); return (error); error: MPASS(error != 0 && *parse_error != NULL); - drop_conf(conf); goto out; }