From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-183.mta0.migadu.com (out-183.mta0.migadu.com [91.218.175.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F3B062DCC03 for ; Mon, 3 Nov 2025 09:07:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.183 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762160865; cv=none; b=MJ+hl5YINrAWMK7wHDrLLndMfQcnlJoam1eLGKS8oSrNZcK+z5ytCzV88cD/t/Us776XlVx9VCrDc1KO81zEs4M0lX3b4NtE09XKmiMyleZkdGXTYrqJwtFZsoM9BpCps3nfjba6WDH3jLY4i8bwaxQrnxPiGBitm6W4S74EUyc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762160865; c=relaxed/simple; bh=HeGonjT1HwnQwLofOy4+RTsZ2YOng/CLAnRlBNw1iSE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=r4yCo0yc1Y8Aw+XBJBmm/Rv5HKKHIPtqCnExVaWc5yO/iiD1IzaI/A6ENrbkGKUr+ug14jCvX0zXQuANB6NYCRykVIziytVIaYw4ciLsC7qYr/9Tpz71+CJLiu/bdkv7rim8jir0AqYqRUuzJKTGEnr05nFCspMnRYT4wygAKVk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=FV7nAraB; arc=none smtp.client-ip=91.218.175.183 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="FV7nAraB" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1762160860; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=qCIcUpFDlcU0Ee2c787K9yU+98dCYjbjMA3KU8RBIWg=; b=FV7nAraBFyX+4SO6UNRkxU9YSvnGmermiWJcSeFKNhgVPQuC0ct5lj3+aOGDjQp33Wlz2T ZLoHvsjKlS/NSEzGsbyOkJpUmDF9ACGYr6EyBsSHdj1SJ8BwaEDrScziuhhnhd9iBdZFcc HGv2c3GP9wfZjPeqhXunZAniAXbZyME= From: Thorsten Blum To: John Johansen , Paul Moore , James Morris , "Serge E. Hallyn" Cc: Thorsten Blum , apparmor@lists.ubuntu.com, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH RESEND] apparmor: Replace sprintf/strcpy with scnprintf/strscpy in aa_policy_init Date: Mon, 3 Nov 2025 10:06:01 +0100 Message-ID: <20251103090601.1737-2-thorsten.blum@linux.dev> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT strcpy() is deprecated and sprintf() does not perform bounds checking either. Although an overflow is unlikely, it's better to proactively avoid it by using the safer strscpy() and scnprintf(), respectively. Additionally, unify memory allocation for 'hname' to simplify and improve aa_policy_init(). Link: https://github.com/KSPP/linux/issues/88 Reviewed-by: Serge Hallyn Signed-off-by: Thorsten Blum --- security/apparmor/lib.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/security/apparmor/lib.c b/security/apparmor/lib.c index 82dbb97ad406..acf7f5189bec 100644 --- a/security/apparmor/lib.c +++ b/security/apparmor/lib.c @@ -478,19 +478,17 @@ bool aa_policy_init(struct aa_policy *policy, const char *prefix, const char *name, gfp_t gfp) { char *hname; + size_t hname_sz; + hname_sz = (prefix ? strlen(prefix) + 2 : 0) + strlen(name) + 1; /* freed by policy_free */ - if (prefix) { - hname = aa_str_alloc(strlen(prefix) + strlen(name) + 3, gfp); - if (hname) - sprintf(hname, "%s//%s", prefix, name); - } else { - hname = aa_str_alloc(strlen(name) + 1, gfp); - if (hname) - strcpy(hname, name); - } + hname = aa_str_alloc(hname_sz, gfp); if (!hname) return false; + if (prefix) + scnprintf(hname, hname_sz, "%s//%s", prefix, name); + else + strscpy(hname, name, hname_sz); policy->hname = hname; /* base.name is a substring of fqname */ policy->name = basename(policy->hname); -- 2.51.1