From patchwork Fri May 3 20:54:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Moyer X-Patchwork-Id: 13653421 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 8B5672E83F for ; Fri, 3 May 2024 20:55:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714769712; cv=none; b=pFhZTKtFN+jHoLqFlsgyOAsjJKV0rZ0ChWrBYeF66LB/g+G6anBCHKhBABljctOuzAn8z0WqEAMddyGdVd35RQK9v6ng3JpqpzJMDU6EDfxlI72rc+5G0+tnLRfTSUOBkzhEmidqvBnTSDxJc7II5YKSvOPDyZ+TvUnxgoMThi4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714769712; c=relaxed/simple; bh=TySOaYvtiDubn4OiFci+FOQ0fzfp7jgcGeR4UoRwwO4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=J3K/vF25y2rcfMrALWlL5LMAIykNKZBjTSF7w6io65qNmbebjI0ucP640FwhqaucLzvNv+P3IzX8dD2ixpTNHQ08/Ix98XryZBOlxU670BhXCKZ5a4mJpAKM5oKLQHdb4AM3V17fevX7ZE7CKRChrt9AKaZOvNh2EkKbBdG6EAw= 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=MrfzPbsk; arc=none smtp.client-ip=170.10.129.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="MrfzPbsk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714769709; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2SKVRu9IYXK1vWA8iKiLPq8WQ+fJK9aW5aGkYUZjjCE=; b=MrfzPbskuhnUthU3pGaG8F2tMuflH0niE4hBtrftPCpRMFPDZUSztZRHQYIu4RaLzGBfIx 3hPnmPE6X2aB0YcG155CeB103fEa08g0cql88sOgZ4YuVcA7xanZiOe21RzGbQoIOByWwI i5/iTMtZDjUX9BxNc93d1dLEGUFqjYk= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-169-8ZIiMeg9Opqud_xOmppMLA-1; Fri, 03 May 2024 16:55:06 -0400 X-MC-Unique: 8ZIiMeg9Opqud_xOmppMLA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0A75D834FAF; Fri, 3 May 2024 20:55:06 +0000 (UTC) Received: from segfault.usersys.redhat.com (unknown [10.22.16.155]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F00A8492BC6; Fri, 3 May 2024 20:55:05 +0000 (UTC) Received: by segfault.usersys.redhat.com (Postfix, from userid 3734) id 9661C22B0C65; Fri, 3 May 2024 16:55:05 -0400 (EDT) From: jmoyer@redhat.com To: nvdimm@lists.linux.dev Cc: vishal.l.verma@intel.com, Jeff Moyer Subject: [PATCH ndctl 1/2] ndctl/keys.c: don't leak fd in error cases Date: Fri, 3 May 2024 16:54:55 -0400 Message-ID: <20240503205456.80004-2-jmoyer@redhat.com> In-Reply-To: <20240503205456.80004-1-jmoyer@redhat.com> References: <20240503205456.80004-1-jmoyer@redhat.com> Precedence: bulk X-Mailing-List: nvdimm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com From: Jeff Moyer Static analysis points out that fd is leaked in some cases. The change to the while loop is optional. I only did that to make the code consistent. Signed-off-by: Jeff Moyer Reviewed-by: Vishal Verma --- ndctl/keys.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ndctl/keys.c b/ndctl/keys.c index 2c1f474..cc55204 100644 --- a/ndctl/keys.c +++ b/ndctl/keys.c @@ -108,7 +108,7 @@ char *ndctl_load_key_blob(const char *path, int *size, const char *postfix, struct stat st; ssize_t read_bytes = 0; int rc, fd; - char *blob, *pl, *rdptr; + char *blob = NULL, *pl, *rdptr; char prefix[] = "load "; bool need_prefix = false; @@ -125,16 +125,16 @@ char *ndctl_load_key_blob(const char *path, int *size, const char *postfix, rc = fstat(fd, &st); if (rc < 0) { fprintf(stderr, "stat: %s\n", strerror(errno)); - return NULL; + goto out_close; } if ((st.st_mode & S_IFMT) != S_IFREG) { fprintf(stderr, "%s not a regular file\n", path); - return NULL; + goto out_close; } if (st.st_size == 0 || st.st_size > 4096) { fprintf(stderr, "Invalid blob file size\n"); - return NULL; + goto out_close; } *size = st.st_size; @@ -166,15 +166,13 @@ char *ndctl_load_key_blob(const char *path, int *size, const char *postfix, fprintf(stderr, "Failed to read from blob file: %s\n", strerror(errno)); free(blob); - close(fd); - return NULL; + blob = NULL; + goto out_close; } read_bytes += rc; rdptr += rc; } while (read_bytes != st.st_size); - close(fd); - if (postfix) { pl += read_bytes; *pl = ' '; @@ -182,6 +180,8 @@ char *ndctl_load_key_blob(const char *path, int *size, const char *postfix, rc = sprintf(pl, "keyhandle=%s", postfix); } +out_close: + close(fd); return blob; } From patchwork Fri May 3 20:54:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Moyer X-Patchwork-Id: 13653420 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 2928B2AEEE for ; Fri, 3 May 2024 20:55:08 +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=1714769710; cv=none; b=NaD9XyGeRPDIqGa0eMxHxqJigdbRw3s3g2VouUgD0DYbs9yPHWDbkcqW7Vt4oR1wjc/dtZFHYk0as+rRqk1bVdA1PLWINMGIEdLgIZqQw4iJApkn1yF5ZM0dFY10IqUSbqxTqXIPr2bcZsxaGTxmA3tvBBLBfIXncnFnJiWPJh8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714769710; c=relaxed/simple; bh=8bmKKGIvBU3vFi9IDrqZuvSNw/63iF7jBSmhvkv9pgw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=e1AZ/3s7JV0goHsPgjq/kb6r4rVjt9S1pZvBcEEvSSkmXwJZwjV1LGT9DUztJHYzrGxMcrUHejkwQ/EwwcjfCp7mAWuQ6YPiU+hWSGFvagUrTXE2inUFf+D6IKg/3nQ7Z2JiHHbvK7z/0XqsqTgK3UyQIYxiGzN7oHte3u7+Pc4= 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=Mg3ytctq; 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="Mg3ytctq" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714769708; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0XAqQg5UTwgYoO3YHPxjIROLrELT1T8YZe2qeJ9e1xg=; b=Mg3ytctqui6VkPpqh9wYqC86/j+kW4WLsAgnnTe39EKnKlnRUxQeNXN7n7E6UKvRDjLfRD sKOTKE47Nu9zu2iXf5A7Qz8TwLpwOn2peQ/4hdge7L4E4bei+V/lbG3mnTiMdR9A2MuGRS in/qOsOg8iw1UdXlqsZav1bsgguqgUk= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-682-PuB1KeXAPvupgosdwu2ZZQ-1; Fri, 03 May 2024 16:55:06 -0400 X-MC-Unique: PuB1KeXAPvupgosdwu2ZZQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id AAF3B29AB3F1; Fri, 3 May 2024 20:55:06 +0000 (UTC) Received: from segfault.usersys.redhat.com (unknown [10.22.16.155]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 956CBC13FA3; Fri, 3 May 2024 20:55:06 +0000 (UTC) Received: by segfault.usersys.redhat.com (Postfix, from userid 3734) id 3E99822B0C65; Fri, 3 May 2024 16:55:06 -0400 (EDT) From: jmoyer@redhat.com To: nvdimm@lists.linux.dev Cc: vishal.l.verma@intel.com, Jeff Moyer Subject: [PATCH ndctl 2/2] libndctl.c: major and minor numbers are unsigned Date: Fri, 3 May 2024 16:54:56 -0400 Message-ID: <20240503205456.80004-3-jmoyer@redhat.com> In-Reply-To: <20240503205456.80004-1-jmoyer@redhat.com> References: <20240503205456.80004-1-jmoyer@redhat.com> Precedence: bulk X-Mailing-List: nvdimm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com From: Jeff Moyer Static analysis points out that the cast of bus->major and bus->minor to a signed type in the call to parent_dev_path could result in a negative number. I sincerely doubt we'll see major and minor numbers that large, but let's fix it. Signed-off-by: Jeff Moyer Reviewed-by: Vishal Verma --- ndctl/lib/libndctl.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ndctl/lib/libndctl.c b/ndctl/lib/libndctl.c index ddbdd9a..f75dbd4 100644 --- a/ndctl/lib/libndctl.c +++ b/ndctl/lib/libndctl.c @@ -710,11 +710,12 @@ NDCTL_EXPORT void ndctl_set_log_priority(struct ndctl_ctx *ctx, int priority) daxctl_set_log_priority(ctx->daxctl_ctx, priority); } -static char *__dev_path(char *type, int major, int minor, int parent) +static char *__dev_path(char *type, unsigned int major, unsigned int minor, + int parent) { char *path, *dev_path; - if (asprintf(&path, "/sys/dev/%s/%d:%d%s", type, major, minor, + if (asprintf(&path, "/sys/dev/%s/%u:%u%s", type, major, minor, parent ? "/device" : "") < 0) return NULL; @@ -723,7 +724,7 @@ static char *__dev_path(char *type, int major, int minor, int parent) return dev_path; } -static char *parent_dev_path(char *type, int major, int minor) +static char *parent_dev_path(char *type, unsigned int major, unsigned int minor) { return __dev_path(type, major, minor, 1); }