From patchwork Wed Jan 29 07:18:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viktor Malik X-Patchwork-Id: 13953483 X-Patchwork-Delegate: bpf@iogearbox.net Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 D11D9179BC for ; Wed, 29 Jan 2025 07:19:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738135159; cv=none; b=Rpu6D9RMZiWjCK+wkBDPgPhXFIhPU2mKHUF3QK4Thu5CkPwunvpHu53TQwSYi/OYGURvJpEv0nlN3iI0nc+PVezYSaxi0JLU0OUDKcSckZ96QbUGJQg1EybU/cQ+8TQQ+UD8oQT3c0bQduHfanEficLXeW6rznnEj92GbtGCPuo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738135159; c=relaxed/simple; bh=/JRg8wT6bLVapHrvU2diywTnXgm1LrV/x0XHglzXt6o=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=JFwyyMeEvvUGTNZSi3n/MTlcgn5Xg892lMHtJeij9rvNjazcAuRkIWcHlNYmDxHcQ1xGNNi5qCwD2gfA3aoTDs5vs089wpF34+UFavY4GhF3u76TkJmPqjFu8kWoKPlcjvG36MRPTu/X5kvHMfWGroxBcTqhBu42Qf8p1jLDZaA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Ue0nY9pB; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Ue0nY9pB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738135156; 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=+DEG/qJWgMITwb5a8q+oCSsM9uuHMf8EwMG1aZjuGcg=; b=Ue0nY9pBypPF9VXzXwv6tQTJY8M9LJAt4U17tVYPjEHUQdPyJUcI9lwuIhYjes1yEjOf3q pPxV0yoYJd5zP6u3OAv5sIwRxtpYeAKFmtLD8N1IAMc2Qez7WhEAv/gbhS/OgNFpdllCZ2 ybd7I++KQO4a/lDEYhoY+OqeMbT07w4= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-2-f3irhiwfM1COcRJT4hORow-1; Wed, 29 Jan 2025 02:19:10 -0500 X-MC-Unique: f3irhiwfM1COcRJT4hORow-1 X-Mimecast-MFC-AGG-ID: f3irhiwfM1COcRJT4hORow Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 21EA619560AB; Wed, 29 Jan 2025 07:19:08 +0000 (UTC) Received: from vmalik-fedora.redhat.com (unknown [10.45.224.79]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1E5851800352; Wed, 29 Jan 2025 07:19:01 +0000 (UTC) From: Viktor Malik To: bpf@vger.kernel.org Cc: Quentin Monnet , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Viktor Malik Subject: [PATCH bpf-next] bpftool: Fix readlink usage in get_fd_type Date: Wed, 29 Jan 2025 08:18:57 +0100 Message-ID: <20250129071857.75182-1-vmalik@redhat.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Patchwork-Delegate: bpf@iogearbox.net The `readlink(path, buf, sizeof(buf))` call reads at most sizeof(buf) bytes and *does not* append null-terminator to buf. With respect to that, fix two pieces in get_fd_type: 1. Change the truncation check to contain sizeof(buf) rather than sizeof(path). 2. Append null-terminator to buf. Reported by Coverity. Signed-off-by: Viktor Malik Reviewed-by: Quentin Monnet --- tools/bpf/bpftool/common.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c index 9b75639434b8..0a764426d935 100644 --- a/tools/bpf/bpftool/common.c +++ b/tools/bpf/bpftool/common.c @@ -461,10 +461,11 @@ int get_fd_type(int fd) p_err("can't read link type: %s", strerror(errno)); return -1; } - if (n == sizeof(path)) { + if (n == sizeof(buf)) { p_err("can't read link type: path too long!"); return -1; } + buf[n] = '\0'; if (strstr(buf, "bpf-map")) return BPF_OBJ_MAP;