Skip to contentSkip to conversation

ethtool: fix possible NULL dereference in main() via argp

PATCH In review [email protected] 2 participants 2 messages May 18, 2025 – Jun 10, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ethtool: fix possible NULL dereference in main() via argp

From: AntonMoryakov <[email protected]>

Static analyzer (Svace) reported a possible null pointer dereference in main(), where the pointer 'argp' is dereferenced without checking whether it is NULL. Specifically, if 'argc' is 0 or reduced to 0 by parsing global options, then '*argp' would cause undefined behavior.

This patch adds a NULL check for 'argp' before calling find_option().

This resolves:
DEREF_AFTER_NULL.EX.COND: ethtool.c:6391

Found by Svace static analysis tool.

---
 ethtool.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ethtool.c +1 -1
index 327a2da..4601051 100644
@@ -6634,7 +6634,7 @@ int main(int argc, char **argp)
 	 * name to get settings for (which we don't expect to begin
 	 * with '-').
 	 */
-	if (!*argp)
+	if (!argp || !*argp)
 		exit_bad_args();
 
 	k = find_option(*argp);
Signature · 1 line
2.34.1
#1 lore raw

Dne Sun, May 18, 2025 at 04:13:32PM GMT, [email protected] napsal:

Quoted 25 lines

From: AntonMoryakov <[email protected]>

Static analyzer (Svace) reported a possible null pointer dereference in main(), where the pointer 'argp' is dereferenced without checking whether it is NULL. Specifically, if 'argc' is 0 or reduced to 0 by parsing global options, then '*argp' would cause undefined behavior.

This patch adds a NULL check for 'argp' before calling find_option().

This resolves:
DEREF_AFTER_NULL.EX.COND: ethtool.c:6391

Found by Svace static analysis tool.

---
 ethtool.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ethtool.c +0 -0
index 327a2da..4601051 100644

ethtool.c int main(int argc, char **argp) · line 6640

Quoted 8 lines
ethtool.c +1 -1
@@ -6634,7 +6634,7 @@ int main(int argc, char **argp)
 	 * name to get settings for (which we don't expect to begin
 	 * with '-').
 	 */
-	if (!*argp)
+	if (!argp || !*argp)
 		exit_bad_args();
 
 	k = find_option(*argp);

This doesn't seem right. First, the ISO C standard guarantees that argv[argc] is a null pointer and if argc > 0, then the array members up to argv[argc - 1] are pointers to strings. This IMHO implies that the value of argp here cannot really be null.

Second, even if we wanted to be protected against such incorrect implementation, there would be no point doing it here, after we already worked with argp through all the block processing global options. It would IMHO make much more sense to check it right at the beginning.

Michal