Skip to contentSkip to conversation

target/ppc/kvm: Skip ".." directory in kvmppc_find_cpu_dt

PATCH In review Murilo Opsfelder Araujo 3 participants 5 messages Jul 11, 2022 – Jul 12, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/ppc/kvm: Skip ".." directory in kvmppc_find_cpu_dt

Some systems have /proc/device-tree/cpus/../clock-frequency. However, this is not the expected path for a CPU device tree directory.

---
 target/ppc/kvm.c | 6 ++++++
 1 file changed, 6 insertions(+)
target/ppc/kvm.c +6 -0
index 6eed466f80..c8485a5cc0 100644
@@ -1877,6 +1877,12 @@ static int kvmppc_find_cpu_dt(char *buf, int buf_len)
     buf[0] = '\0';
     while ((dirp = readdir(dp)) != NULL) {
         FILE *f;
+
+        /* Don't accidentally read from the upper directory */
+        if (strcmp(dirp->d_name, "..") == 0) {
+            continue;
+        }
+
         snprintf(buf, buf_len, "%s%s/clock-frequency", PROC_DEVTREE_CPU,
                  dirp->d_name);
         f = fopen(buf, "r");
Signature · 1 line
2.36.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Murilo Opsfelder Araujo

Some systems have /proc/device-tree/cpus/../clock-frequency. However, this is not the expected path for a CPU device tree directory.

---

Quoted 20 lines
  target/ppc/kvm.c | 6 ++++++
  1 file changed, 6 insertions(+)
target/ppc/kvm.c +6 -0
index 6eed466f80..c8485a5cc0 100644
@@ -1877,6 +1877,12 @@ static int kvmppc_find_cpu_dt(char *buf, int buf_len)
      buf[0] = '\0';
      while ((dirp = readdir(dp)) != NULL) {
          FILE *f;
+
+        /* Don't accidentally read from the upper directory */
+        if (strcmp(dirp->d_name, "..") == 0) {
+            continue;
+        }
+
          snprintf(buf, buf_len, "%s%s/clock-frequency", PROC_DEVTREE_CPU,
                   dirp->d_name);
          f = fopen(buf, "r");
#2 lore raw
In reply to Murilo Opsfelder Araujo
Quoted 14 lines · Murilo Opsfelder Araujo

Some systems have /proc/device-tree/cpus/../clock-frequency. However, this is not the expected path for a CPU device tree directory.

---
 target/ppc/kvm.c | 6 ++++++
 1 file changed, 6 insertions(+)
target/ppc/kvm.c +0 -0
index 6eed466f80..c8485a5cc0 100644

target/ppc/kvm.c static int kvmppc_find_cpu_dt(char *buf, int buf_len) · line 1882

Quoted 6 lines
target/ppc/kvm.c +3 -0
@@ -1877,6 +1877,12 @@ static int kvmppc_find_cpu_dt(char *buf, int buf_len)
     buf[0] = '\0';
     while ((dirp = readdir(dp)) != NULL) {
         FILE *f;
+
+        /* Don't accidentally read from the upper directory */
+        if (strcmp(dirp->d_name, "..") == 0) {

It might not be causing problems now, but it would be technically more correct to also skip ".", wouldn't it?

Quoted 6 lines
target/ppc/kvm.c +3 -0
+            continue;
+        }
+
         snprintf(buf, buf_len, "%s%s/clock-frequency", PROC_DEVTREE_CPU,
                  dirp->d_name);
         f = fopen(buf, "r");
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#3 lore raw
Quoted 10 lines · David Gibson
Quoted 9 lines · Murilo Opsfelder Araujo

Some systems have /proc/device-tree/cpus/../clock-frequency. However, this is not the expected path for a CPU device tree directory.

---
  target/ppc/kvm.c | 6 ++++++
  1 file changed, 6 insertions(+)
Quoted 14 lines

target/ppc/kvm.c static int kvmppc_find_cpu_dt(char *buf, int buf_len) · line 1882

Quoted 11 lines
target/ppc/kvm.c +3 -0
index 6eed466f80..c8485a5cc0 100644
@@ -1877,6 +1877,12 @@ static int kvmppc_find_cpu_dt(char *buf, int buf_len)
      buf[0] = '\0';
      while ((dirp = readdir(dp)) != NULL) {
          FILE *f;
+
+        /* Don't accidentally read from the upper directory */
+        if (strcmp(dirp->d_name, "..") == 0) {

It might not be causing problems now, but it would be technically more correct to also skip ".", wouldn't it?

Given that the use of this function is inside kvmppc_read_int_cpu_dt(), which is used to read a property that belongs to a CPU node, I believe you're right. It's better to avoid returning "PROC_DEVTREE_CPU" as well.

Murilo, can you please re-send it skipping both ".." and "." ? Better be on the safe side.

Daniel

Quoted 8 lines
Quoted 7 lines
target/ppc/kvm.c +3 -0
+            continue;
+        }
+
          snprintf(buf, buf_len, "%s%s/clock-frequency", PROC_DEVTREE_CPU,
                   dirp->d_name);
          f = fopen(buf, "r");
#4 lore raw

Hi, Daniel, David.

Quoted 32 lines · Daniel Henrique Barboza
Quoted 10 lines · David Gibson
Quoted 9 lines · Murilo Opsfelder Araujo

Some systems have /proc/device-tree/cpus/../clock-frequency. However, this is not the expected path for a CPU device tree directory.

---
  target/ppc/kvm.c | 6 ++++++

  1 file changed, 6 insertions(+)
Quoted 14 lines

target/ppc/kvm.c static int kvmppc_find_cpu_dt(char *buf, int buf_len) · line 1882

Quoted 11 lines
target/ppc/kvm.c +3 -0
index 6eed466f80..c8485a5cc0 100644
@@ -1877,6 +1877,12 @@ static int kvmppc_find_cpu_dt(char *buf, int buf_len)
       buf[0] = '\0';
       while ((dirp = readdir(dp)) != NULL) {
           FILE *f;
+
+        /* Don't accidentally read from the upper directory */
+        if (strcmp(dirp->d_name, "..") == 0) {

It might not be causing problems now, but it would be technically more correct to also skip ".", wouldn't it?

Given that the use of this function is inside kvmppc_read_int_cpu_dt(), which is used to read a property that belongs to a CPU node, I believe you're right. It's better to avoid returning "PROC_DEVTREE_CPU" as well.

Quoted 5 lines

Murilo, can you please re-send it skipping both ".." and "." ? Better be on the safe side.

Daniel

I've sent v2:

    https://lore.kernel.org/qemu-devel/[email protected]/

Thank you for reviewing.

Signature · 1 line
Murilo