Skip to contentSkip to conversation

kernfs: fix potential null-ptr-deref in kernfs_path_from_node_locked()

PATCH In review Zhen Lei 3 participants 9 messages Nov 23, 2022 – Nov 28, 2022 kernfs

lore
Share & tools
lore

Zhen Lei

author
#0 lore raw

PATCH 1/1 kernfs: fix potential null-ptr-deref in kernfs_path_from_node_locked()

Ensure that the 'buf' is not empty before strlcpy() uses it.

Commit bbe70e4e4211 ("fs: kernfs: Fix possible null-pointer dereferences in kernfs_path_from_node_locked()") first noticed this, but it didn't fix it completely.

Fixes 9f6df573a404 ("kernfs: Add API to generate relative kernfs path")
---
 fs/kernfs/dir.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/kernfs/dir.c +3 -3
index f33b3baad07cb36..b2422265c86edf2 100644
@@ -140,6 +140,9 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
 	size_t depth_from, depth_to, len = 0;
 	int i, j;
 
+	if (!buf)
+		return -EINVAL;
+
 	if (!kn_to)
 		return strlcpy(buf, "(null)", buflen);
 
@@ -149,9 +152,6 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
 	if (kn_from == kn_to)
 		return strlcpy(buf, "/", buflen);
 
-	if (!buf)
-		return -EINVAL;
-
 	common = kernfs_common_ancestor(kn_from, kn_to);
 	if (WARN_ON(!common))
 		return -EINVAL;
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 8 lines · Zhen Lei

Ensure that the 'buf' is not empty before strlcpy() uses it.

Commit bbe70e4e4211 ("fs: kernfs: Fix possible null-pointer dereferences in kernfs_path_from_node_locked()") first noticed this, but it didn't fix it completely.

Fixes 9f6df573a404 ("kernfs: Add API to generate relative kernfs path")

I think the right thing to do is removing that if. It makes no sense to call that function with NULL buf and the fact that nobody reported crashes on NULL buf indicates that we in fact never do.

Thanks.

Signature · 1 line
tejun
#2 lore raw
Quoted 7 lines · Tejun Heo
Quoted 6 lines · Zhen Lei

Ensure that the 'buf' is not empty before strlcpy() uses it.

Commit bbe70e4e4211 ("fs: kernfs: Fix possible null-pointer dereferences in kernfs_path_from_node_locked()") first noticed this, but it didn't fix it completely.

Quoted 6 lines
Quoted 2 lines
Fixes 9f6df573a404 ("kernfs: Add API to generate relative kernfs path")

I think the right thing to do is removing that if. It makes no sense to call that function with NULL buf and the fact that nobody reported crashes on NULL buf indicates that we in fact never do.

OK.

How about I remove "buf[0] = '\0';" too? It seems to be a useless operation. When 'kn_from' and 'kn_to' have a common ancestor, there must be a path from 'kn_from' to 'kn_to', and strlcpy() always fills in the terminator correctly, even if the buf is too small to save the first path node.

static void test(void)

{
        char buf[4];
        int i, n, buflen;

        buflen = 1;
        n = strlcpy(buf, "string", buflen);
        for (i = 0; i < buflen; i++)
                printk("%d: %02x\n", i, buf[i]);
        printk("n=%d\n\n", n);

        buflen = sizeof(buf);
        n = strlcpy(buf, "string", buflen);
        for (i = 0; i < buflen; i++)
                printk("%d: %02x\n", i, buf[i]);
        printk("n=%d\n", n);
}

Output:

[   33.691497] 0: 00
[   33.691569] n=6
[   33.691595] 0: 73
[   33.691622] 1: 74
[   33.691630] 2: 72
[   33.691637] 3: 00
[   33.691650] n=6
Quoted 3 lines

Thanks.

Signature · 2 lines
Regards,
  Zhen Lei
#3 lore raw
Quoted 17 lines · Leizhen (ThunderTown)
Quoted 7 lines · Tejun Heo
Quoted 6 lines · Zhen Lei

Ensure that the 'buf' is not empty before strlcpy() uses it.

Commit bbe70e4e4211 ("fs: kernfs: Fix possible null-pointer dereferences in kernfs_path_from_node_locked()") first noticed this, but it didn't fix it completely.

Quoted 6 lines
Quoted 2 lines
Fixes 9f6df573a404 ("kernfs: Add API to generate relative kernfs path")

I think the right thing to do is removing that if. It makes no sense to call that function with NULL buf and the fact that nobody reported crashes on NULL buf indicates that we in fact never do.

Quoted 6 lines

OK.

How about I remove "buf[0] = '\0';" too? It seems to be a useless operation. When 'kn_from' and 'kn_to' have a common ancestor, there must be a path from 'kn_from' to 'kn_to', and strlcpy() always fills in the terminator correctly, even if the buf is too small to save the first path node.

Sorry, I misanalyzed. The length used by "len < buflen ? buflen - len : 0" may be zero.

Quoted 33 lines

static void test(void)

{
        char buf[4];
        int i, n, buflen;

        buflen = 1;
        n = strlcpy(buf, "string", buflen);
        for (i = 0; i < buflen; i++)
                printk("%d: %02x\n", i, buf[i]);
        printk("n=%d\n\n", n);

        buflen = sizeof(buf);
        n = strlcpy(buf, "string", buflen);
        for (i = 0; i < buflen; i++)
                printk("%d: %02x\n", i, buf[i]);
        printk("n=%d\n", n);
}

Output:

[   33.691497] 0: 00
[   33.691569] n=6
[   33.691595] 0: 73
[   33.691622] 1: 74
[   33.691630] 2: 72
[   33.691637] 3: 00
[   33.691650] n=6
Quoted 3 lines

Thanks.

Signature · 2 lines
Regards,
  Zhen Lei
#4 lore raw
Quoted 22 lines · Leizhen (ThunderTown)
Quoted 19 lines · Leizhen (ThunderTown)
Quoted 7 lines · Tejun Heo
Quoted 6 lines · Zhen Lei

Ensure that the 'buf' is not empty before strlcpy() uses it.

Commit bbe70e4e4211 ("fs: kernfs: Fix possible null-pointer dereferences in kernfs_path_from_node_locked()") first noticed this, but it didn't fix it completely.

Quoted 6 lines
Quoted 2 lines
Fixes 9f6df573a404 ("kernfs: Add API to generate relative kernfs path")

I think the right thing to do is removing that if. It makes no sense to call that function with NULL buf and the fact that nobody reported crashes on NULL buf indicates that we in fact never do.

OK.

Quoted 7 lines
Quoted 4 lines

How about I remove "buf[0] = '\0';" too? It seems to be a useless operation. When 'kn_from' and 'kn_to' have a common ancestor, there must be a path from 'kn_from' to 'kn_to', and strlcpy() always fills in the terminator correctly, even if the buf is too small to save the first path node.

Sorry, I misanalyzed. The length used by "len < buflen ? buflen - len : 0" may be zero.

Ah, my brain is unstable today. The initial value of len is 0. So "buf[0] = '\0';" can still be safely removed.

Quoted 35 lines
Quoted 34 lines

static void test(void)

{
        char buf[4];
        int i, n, buflen;

        buflen = 1;
        n = strlcpy(buf, "string", buflen);
        for (i = 0; i < buflen; i++)
                printk("%d: %02x\n", i, buf[i]);
        printk("n=%d\n\n", n);

        buflen = sizeof(buf);
        n = strlcpy(buf, "string", buflen);
        for (i = 0; i < buflen; i++)
                printk("%d: %02x\n", i, buf[i]);
        printk("n=%d\n", n);
}

Output:

[   33.691497] 0: 00
[   33.691569] n=6
[   33.691595] 0: 73
[   33.691622] 1: 74
[   33.691630] 2: 72
[   33.691637] 3: 00
[   33.691650] n=6
Quoted 3 lines

Thanks.

Signature · 2 lines
Regards,
  Zhen Lei
#5 lore raw
Quoted 16 lines · Leizhen (ThunderTown)
Quoted 13 lines · Leizhen (ThunderTown)
Quoted 10 lines · Leizhen (ThunderTown)
Quoted 7 lines · Tejun Heo
Quoted 6 lines · Zhen Lei

Ensure that the 'buf' is not empty before strlcpy() uses it.

Commit bbe70e4e4211 ("fs: kernfs: Fix possible null-pointer dereferences in kernfs_path_from_node_locked()") first noticed this, but it didn't fix it completely.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Fixes 9f6df573a404 ("kernfs: Add API to generate relative kernfs path")

I think the right thing to do is removing that if. It makes no sense to call that function with NULL buf and the fact that nobody reported crashes on NULL buf indicates that we in fact never do.

kernfs_path_from_node

    -->kernfs_path_from_node_locked

EXPORT_SYMBOL_GPL(kernfs_path_from_node)

I've rethought it. The export APIs need to do null pointer check, right?

Quoted 50 lines
Quoted 10 lines
Quoted 7 lines

OK.

How about I remove "buf[0] = '\0';" too? It seems to be a useless operation. When 'kn_from' and 'kn_to' have a common ancestor, there must be a path from 'kn_from' to 'kn_to', and strlcpy() always fills in the terminator correctly, even if the buf is too small to save the first path node.

Sorry, I misanalyzed. The length used by "len < buflen ? buflen - len : 0" may be zero.

Ah, my brain is unstable today. The initial value of len is 0. So "buf[0] = '\0';" can still be safely removed.

Quoted 35 lines
Quoted 34 lines

static void test(void)

{
        char buf[4];
        int i, n, buflen;

        buflen = 1;
        n = strlcpy(buf, "string", buflen);
        for (i = 0; i < buflen; i++)
                printk("%d: %02x\n", i, buf[i]);
        printk("n=%d\n\n", n);

        buflen = sizeof(buf);
        n = strlcpy(buf, "string", buflen);
        for (i = 0; i < buflen; i++)
                printk("%d: %02x\n", i, buf[i]);
        printk("n=%d\n", n);
}

Output:

[   33.691497] 0: 00
[   33.691569] n=6
[   33.691595] 0: 73
[   33.691622] 1: 74
[   33.691630] 2: 72
[   33.691637] 3: 00
[   33.691650] n=6
Quoted 3 lines

Thanks.

Signature · 2 lines
Regards,
  Zhen Lei
#6 lore raw
Quoted 26 lines · Leizhen (ThunderTown)
Quoted 16 lines · Leizhen (ThunderTown)
Quoted 13 lines · Leizhen (ThunderTown)
Quoted 10 lines · Leizhen (ThunderTown)
Quoted 7 lines · Tejun Heo
Quoted 6 lines · Zhen Lei

Ensure that the 'buf' is not empty before strlcpy() uses it.

Commit bbe70e4e4211 ("fs: kernfs: Fix possible null-pointer dereferences in kernfs_path_from_node_locked()") first noticed this, but it didn't fix it completely.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Fixes 9f6df573a404 ("kernfs: Add API to generate relative kernfs path")

I think the right thing to do is removing that if. It makes no sense to call that function with NULL buf and the fact that nobody reported crashes on NULL buf indicates that we in fact never do.

Quoted 6 lines

kernfs_path_from_node

    -->kernfs_path_from_node_locked

EXPORT_SYMBOL_GPL(kernfs_path_from_node)

I've rethought it. The export APIs need to do null pointer check, right?

No, callers should get this right. Are there any in-tree ones that do not?

thanks,

greg k-h

#7 lore raw
Quoted 30 lines · Greg Kroah-Hartman
Quoted 29 lines · Leizhen (ThunderTown)
Quoted 16 lines · Leizhen (ThunderTown)
Quoted 13 lines · Leizhen (ThunderTown)
Quoted 10 lines · Leizhen (ThunderTown)
Quoted 7 lines · Tejun Heo
Quoted 6 lines · Zhen Lei

Ensure that the 'buf' is not empty before strlcpy() uses it.

Commit bbe70e4e4211 ("fs: kernfs: Fix possible null-pointer dereferences in kernfs_path_from_node_locked()") first noticed this, but it didn't fix it completely.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Fixes 9f6df573a404 ("kernfs: Add API to generate relative kernfs path")

I think the right thing to do is removing that if. It makes no sense to call that function with NULL buf and the fact that nobody reported crashes on NULL buf indicates that we in fact never do.

kernfs_path_from_node

    -->kernfs_path_from_node_locked
Quoted 6 lines
Quoted 3 lines

EXPORT_SYMBOL_GPL(kernfs_path_from_node)

I've rethought it. The export APIs need to do null pointer check, right?

No, callers should get this right. Are there any in-tree ones that do not?

Thanks. I got it.

Quoted 6 lines

thanks,

greg k-h
.

Signature · 2 lines
Regards,
  Zhen Lei
#8 lore raw
In reply to Leizhen (ThunderTown)
Quoted 26 lines · Leizhen (ThunderTown)
Quoted 16 lines · Leizhen (ThunderTown)
Quoted 13 lines · Leizhen (ThunderTown)
Quoted 10 lines · Leizhen (ThunderTown)
Quoted 7 lines · Tejun Heo
Quoted 6 lines · Zhen Lei

Ensure that the 'buf' is not empty before strlcpy() uses it.

Commit bbe70e4e4211 ("fs: kernfs: Fix possible null-pointer dereferences in kernfs_path_from_node_locked()") first noticed this, but it didn't fix it completely.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Fixes 9f6df573a404 ("kernfs: Add API to generate relative kernfs path")

I think the right thing to do is removing that if. It makes no sense to call that function with NULL buf and the fact that nobody reported crashes on NULL buf indicates that we in fact never do.

Quoted 6 lines

kernfs_path_from_node

    -->kernfs_path_from_node_locked

EXPORT_SYMBOL_GPL(kernfs_path_from_node)

I've rethought it. The export APIs need to do null pointer check, right?

Sorry,I looked at some of the other functions and they didn't check either.

Quoted 52 lines
Quoted 51 lines
Quoted 11 lines
Quoted 8 lines

OK.

How about I remove "buf[0] = '\0';" too? It seems to be a useless operation. When 'kn_from' and 'kn_to' have a common ancestor, there must be a path from 'kn_from' to 'kn_to', and strlcpy() always fills in the terminator correctly, even if the buf is too small to save the first path node.

Sorry, I misanalyzed. The length used by "len < buflen ? buflen - len : 0" may be zero.

Ah, my brain is unstable today. The initial value of len is 0. So "buf[0] = '\0';" can still be safely removed.

Quoted 35 lines
Quoted 34 lines

static void test(void)

{
        char buf[4];
        int i, n, buflen;

        buflen = 1;
        n = strlcpy(buf, "string", buflen);
        for (i = 0; i < buflen; i++)
                printk("%d: %02x\n", i, buf[i]);
        printk("n=%d\n\n", n);

        buflen = sizeof(buf);
        n = strlcpy(buf, "string", buflen);
        for (i = 0; i < buflen; i++)
                printk("%d: %02x\n", i, buf[i]);
        printk("n=%d\n", n);
}

Output:

[   33.691497] 0: 00
[   33.691569] n=6
[   33.691595] 0: 73
[   33.691622] 1: 74
[   33.691630] 2: 72
[   33.691637] 3: 00
[   33.691650] n=6
Quoted 3 lines

Thanks.

Signature · 2 lines
Regards,
  Zhen Lei