|
|
3f927f |
From 721460664afad79e2d96bbcb173eda68eed9743b Mon Sep 17 00:00:00 2001
|
|
|
3f927f |
From: Gerd Rausch <gerd.rausch@oracle.com>
|
|
|
3f927f |
Date: Thu, 18 Oct 2018 11:21:40 -0700
|
|
|
3f927f |
Subject: [PATCH] Fix ambiguous parsing of *node* entries in /sys.
|
|
|
3f927f |
|
|
|
3f927f |
The code used to use strstr(..., "node") while iterating over
|
|
|
3f927f |
sysfs directories such as /sys/devices/system/cpu/cpu*.
|
|
|
3f927f |
It then made an assumption that the entry would start with "node",
|
|
|
3f927f |
which is not necessarily the case (e.g. the "firmware_node" entry).
|
|
|
3f927f |
|
|
|
3f927f |
The code happened to work for as long as the node[0-9]* entry
|
|
|
3f927f |
would be processed before the "firmware_node" entry shows up.
|
|
|
3f927f |
|
|
|
3f927f |
A change to the linux kernel "end_name_hash" function resulted
|
|
|
3f927f |
in a different hash, and ultimately in a different order
|
|
|
3f927f |
by which entries were returned by readdir(3).
|
|
|
3f927f |
|
|
|
3f927f |
This led to the exposure of this bug.
|
|
|
3f927f |
|
|
|
3f927f |
Signed-off-by: Gerd Rausch <gerd.rausch@oracle.com>
|
|
|
3f927f |
---
|
|
|
3f927f |
cputree.c | 11 ++++++++---
|
|
|
3f927f |
numa.c | 5 ++++-
|
|
|
3f927f |
2 files changed, 12 insertions(+), 4 deletions(-)
|
|
|
3f927f |
|
|
|
3f927f |
diff --git a/cputree.c b/cputree.c
|
|
|
3f927f |
index c88143f..f08ce84 100644
|
|
|
3f927f |
--- a/cputree.c
|
|
|
3f927f |
+++ b/cputree.c
|
|
|
3f927f |
@@ -368,9 +368,14 @@ static void do_one_cpu(char *path)
|
|
|
3f927f |
entry = readdir(dir);
|
|
|
3f927f |
if (!entry)
|
|
|
3f927f |
break;
|
|
|
3f927f |
- if (strstr(entry->d_name, "node")) {
|
|
|
3f927f |
- nodeid = strtoul(&entry->d_name[4], NULL, 10);
|
|
|
3f927f |
- break;
|
|
|
3f927f |
+ if (strncmp(entry->d_name, "node", 4) == 0) {
|
|
|
3f927f |
+ char *end;
|
|
|
3f927f |
+ int num;
|
|
|
3f927f |
+ num = strtol(entry->d_name + 4, &end, 10);
|
|
|
3f927f |
+ if (!*end && num >= 0) {
|
|
|
3f927f |
+ nodeid = num;
|
|
|
3f927f |
+ break;
|
|
|
3f927f |
+ }
|
|
|
3f927f |
}
|
|
|
3f927f |
} while (entry);
|
|
|
3f927f |
closedir(dir);
|
|
|
3f927f |
diff --git a/numa.c b/numa.c
|
|
|
3f927f |
index cd67ec8..f0b1a98 100644
|
|
|
3f927f |
--- a/numa.c
|
|
|
3f927f |
+++ b/numa.c
|
|
|
3f927f |
@@ -29,6 +29,7 @@
|
|
|
3f927f |
#include <unistd.h>
|
|
|
3f927f |
#include <stdlib.h>
|
|
|
3f927f |
#include <stdio.h>
|
|
|
3f927f |
+#include <ctype.h>
|
|
|
3f927f |
#include <sys/types.h>
|
|
|
3f927f |
#include <dirent.h>
|
|
|
3f927f |
|
|
|
3f927f |
@@ -115,7 +116,9 @@ void build_numa_node_list(void)
|
|
|
3f927f |
entry = readdir(dir);
|
|
|
3f927f |
if (!entry)
|
|
|
3f927f |
break;
|
|
|
3f927f |
- if ((entry->d_type == DT_DIR) && (strstr(entry->d_name, "node"))) {
|
|
|
3f927f |
+ if ((entry->d_type == DT_DIR) &&
|
|
|
3f927f |
+ (strncmp(entry->d_name, "node", 4) == 0) &&
|
|
|
3f927f |
+ isdigit(entry->d_name[4])) {
|
|
|
3f927f |
add_one_node(entry->d_name);
|
|
|
3f927f |
}
|
|
|
3f927f |
} while (entry);
|
|
|
3f927f |
--
|
|
|
3f927f |
2.21.0
|
|
|
3f927f |
|