From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6603D388E42 for ; Tue, 24 Mar 2026 14:50:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=209.85.221.51 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774363860; cv=pass; b=n8pndAgszek4KXvMHtw0WgmuBDKGKTCIGK4iweC8kDgCpo6KL5TOpe6TCrNJXgCK/Ekt5g6aPNcZWxUW9PHZYDhb3dcvjxvsgP4tykR4YOder56EWN/JKoar2+aGXVjGWifjsw5tLARE3A1+Fb30I5L7r1anqWUWje0SYyNVGYA= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774363860; c=relaxed/simple; bh=3n66akwPL5aH75TB6OiseSZhlNEcpu5V8jrnFridO/Q=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=iuWHV3Ern7hsKc9pYTA8JtMlENp+5YgUF5F4excFc7vVVF7taWAq2ai39da7eWVgtte2n21Xz+DOswkncVzJpwxEhnRjdInyiXlK4dHQq4roItJAgnmLQ/Yxi+cbSzDnDS38evGWmf2bzawQi93oCRBErVeo8nLr4atj1Fn7aww= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=f3D7LOuS; arc=pass smtp.client-ip=209.85.221.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="f3D7LOuS" Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-43b40003d13so3467188f8f.2 for ; Tue, 24 Mar 2026 07:50:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1774363858; cv=none; d=google.com; s=arc-20240605; b=M5Oqe6afeLY11zQ6u5ofGFw9Q3/g9OAY5GyeQYExrqZlw4iTjlm8B5VdxV4OHBX+6v 4hG8UkjERuDaRtiglYouC+0tVBFWy76VEcQ+ZH+xhKwR+5dVtQaoxEKrYmM1wXnJ3ZNH nMoxheapTT6svQIcFXYGbqrZoZntIqME4/2bdnaBytVydQnbM+QyuHA3MNVLlMMHEIP3 5fh4jiOkWKR489Ipsq8y0Yo4/7haa1YKTuFHcD337VsMpYdpbWgzZt7AqOpm0Xe/ZcfI VCD+rudxo3GcyR7cbwPn2JbD49ndI9JXUcIubc5YqgO1tsW2fBqn8XxkkmLxT/5CWxTd Dzlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=OVDnf3LS1Zy+J4cELDWzZkJKr0472zSbdgFTuK1Jc+I=; fh=ysXwQair39W8F4NKdENB0mkbxBrmOMZOWMJp2RRl5n0=; b=HHluoNWjI+7nnFRXjHisreS4qGp5+4PdT3CccaIJeN0X1MVpFA5CiQWmCoqBXoX+cc fEBDeps3J//sFvOvqMi/lPlCDblReeFnkHIZOgGigDA4cLvHabVcYn2MiGIzMyWRs2E4 MqwtinSVgFJ3DeIcySffFMMG0xg5UunBUTgYhbTr5/3df5uXEroYVu5t+3p484O/aK59 Xj1oOLLX4PaGSsMxtJr1ZFwKKWsPUUbucsWsDMBl2nyPO81Ws8G6IY/Vn4bbzzl2cfw9 4oCfxMjA7sG5XesVd6LS9RMXQIbC02art94o8VheTXVlIbY0A1sHSO+WnPnYsdh6Iv+a tjDg==; darn=vger.kernel.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774363858; x=1774968658; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=OVDnf3LS1Zy+J4cELDWzZkJKr0472zSbdgFTuK1Jc+I=; b=f3D7LOuSKSy/VLEGL33L2EmOjyTUGx4WEXmVEyf16nFhWUIi/PGwJKBKHLtCNjCDRH lBvH5bdjjWxKhn1iUCLnzS2ihSuw/oF9m83gStwk5B9hyaOm46pwabNo7SuICLF2xPUc +SOH48A+dodvp7CuDviU71KW1mfRpF5861gvQ3IYPje/ntpJBRg6N5h2GGCLM2wrj/vV +GezvMSCNSLoBc/EKL+B/oWlAxlp3rHUMkRyqd8bFlgp/azohQL1CYhZjWXE1NfRzaGM MnG6z8NHMOGagKHmvP35GRc3ATRKMyXZn9jaXSIpaaqVZpB+JYQL2CDbEm/PU9OAm6Uf sWzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774363858; x=1774968658; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=OVDnf3LS1Zy+J4cELDWzZkJKr0472zSbdgFTuK1Jc+I=; b=bprewQ24QIUpNZLOQjoMp/+Iq5RtCkVeJIdWsB9m54Qa7o+2dCEgbYDSN2PmsP/dsc W6AtSLIW3UsTLECt690RmdNqSZphfPlss1NemTg5LsI8eGt08ih3aJ/GeMaG9NHATo+Y TOI601weDHWFNxOBpeG8/UhyYljbYX8meHsmnr85cfcxecjXQlkN7wL988zkKvqyNA+L UD1BSk8szoocww/AgsZHZfOQw96g7PlojFNpaTpfjKmHKGjcEx8v8+VJDUocXH6Yww5W Wf+V77Pet9aSv+PSJDUUIrX74AYbX/72QQiXeHj6JGRVeofuICrz+8Llg16QrQUYxYX3 qQug== X-Forwarded-Encrypted: i=1; AJvYcCV1DY9lDnXWizAAZ+qRSHdjAqyLqtfx2lncmoZXDnCFO3ItkT+0BSzyLdyFv8QjLRf6ehL0oYM8R9DCdto=@vger.kernel.org X-Gm-Message-State: AOJu0YwGmf194d7+RxNI2WI5b3g6voG8UqnFirCn3lwXD0rEIIS7DJTv NBqYOyRoghL/98fYXFAcwsi62ZfJL0m5wd2Oit0zfmp2aMoaNeuEI4Exzft/ScW5Uu2WIo45C1E TYpEU1s/Yoht3d+sTI/eYcRQ7diZfbR0= X-Gm-Gg: ATEYQzzQKhVpnkBx7MMF9OkNoRXL2c5Z9GkIkI7LcZYM6m2PiVRT2iw7nKgoV3CzGLM bFrV5yIbXeajVZ80yf2iGPhQYIKvmDHskh3mWcDuw05Twou8L4a3nE/qOR90Xo2qZmQ2W555eKu bZ/cztjC4tvC7oNSuQrlWdZJuhbnwtjr/lzT2sxFb/fozIQ0YREjkGZANSbX4RaL+UMCMuNmv6k 9BTemBqVnAkkj0MrFn5GzCpMvoDVb9GfjfFIOUpNMxZDw9Pg/rouF86sdEeuI+ba/NquspyiNEc knTaoWkD1Llylinilzo/fYcoOYNueaYT7uANNYjsybGRmQRBJx036DcL9ZkqmRqTWr6uyKs9ylT I8iRYXQ0GcvYKnlqOgwwKWHUlhFwacz3Meis7 X-Received: by 2002:a5d:5450:0:b0:43b:8023:8b2 with SMTP id ffacd0b85a97d-43b80232811mr4547485f8f.45.1774363857554; Tue, 24 Mar 2026 07:50:57 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20260324053036.it.906-kees@kernel.org> In-Reply-To: <20260324053036.it.906-kees@kernel.org> From: Alexei Starovoitov Date: Tue, 24 Mar 2026 07:50:44 -0700 X-Gm-Features: AQROBzBrEbGkOPmrzb0Te4EzLdynK4wKlfwGXuHnnrrTxceVeo39F1XZrtWmaho Message-ID: Subject: Re: [PATCH v2] libbpf: Replace strncpy() with strnlen()+memcpy() in skel_map_create() To: Kees Cook Cc: Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf , LKML , linux-hardening@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Mar 23, 2026 at 10:30=E2=80=AFPM Kees Cook wrote: > > Replace the deprecated[1] strncpy() with strnlen() on the source > followed by memcpy(). Normally strscpy() would be used in this case, > but skel_internal.h is shared between kernel and userspace tools, and > strscpy() is not available in the userspace build context. > > The source map_name is a NUL-terminated C string (the only caller > passes a 12 character string literal). The destination attr.map_name is > char[BPF_OBJ_NAME_LEN] (16 bytes) in union bpf_attr, passed to the bpf() > syscall. The kernel's bpf_obj_name_cpy() requires a NUL terminator within > the 16-byte field, rejecting names that use all 16 bytes. Valid names > are therefore at most 15 characters. > > The attr is pre-zeroed with memset() at the top of the function, > so the byte at position 15 is always NUL. The copy is bounded to > sizeof(attr.map_name) - 1 (15 bytes) to guarantee NUL-termination is > preserved. This is safe because the kernel would reject a 16-byte > unterminated name anyway, and the only in-tree caller passes > "__loader.map" (12 characters). > > While the original strncpy() would have copied a full 16 bytes from an > overlong name (producing an unterminated field that the syscall rejects), > but this shouldn't be a reachable state. Forcing truncation to 15 bytes, > however, seemed to induce a (unrelated?) BPF selftest failure: > https://github.com/kernel-patches/bpf/actions/runs/23472955268/job/683004= 40546 > > Allow the literal to exceed 15 characters and exactly reproduce the > original strncpy() behavior (potentially lacking a NUL termination). > > Link: https://github.com/KSPP/linux/issues/90 [1] > Signed-off-by: Kees Cook > --- > v2: don't force truncation > v1: https://lore.kernel.org/lkml/20260324040535.work.851-kees@kernel.org= / > Cc: Andrii Nakryiko > Cc: Eduard Zingerman > Cc: Alexei Starovoitov > Cc: Daniel Borkmann > Cc: Martin KaFai Lau > Cc: Song Liu > Cc: Yonghong Song > Cc: John Fastabend > Cc: KP Singh > Cc: Stanislav Fomichev > Cc: Hao Luo > Cc: Jiri Olsa > Cc: > --- > tools/lib/bpf/skel_internal.h | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/tools/lib/bpf/skel_internal.h b/tools/lib/bpf/skel_internal.= h > index 6a8f5c7a02eb..8702d6612978 100644 > --- a/tools/lib/bpf/skel_internal.h > +++ b/tools/lib/bpf/skel_internal.h > @@ -243,7 +243,8 @@ static inline int skel_map_create(enum bpf_map_type m= ap_type, > attr.excl_prog_hash =3D (unsigned long) excl_prog_hash; > attr.excl_prog_hash_size =3D excl_prog_hash_sz; > > - strncpy(attr.map_name, map_name, sizeof(attr.map_name)); > + memcpy(attr.map_name, map_name, > + strnlen(map_name, sizeof(attr.map_name))); wont-fix. pw-bot: cr