Skip to contentSkip to conversation

blktrace: fix dereference after null check

PATCH Superseded Cengiz Can 3 participants 6 messages Mar 3, 2020 – Mar 4, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Cengiz Can

author
#0 lore raw

PATCH 1/1 blktrace: fix dereference after null check

There was a recent change in blktrace.c that added a RCU protection to q->blk_trace in order to fix a use-after-free issue during access.

However the change missed an edge case that can lead to dereferencing of bt pointer even when it's NULL:

```
        bt->act_mask = value; // bt can still be NULL here
```

Added a reassignment into the NULL check block to fix the issue.

Fixes c780e86dd48 ("blktrace: Protect q->blk_trace with RCU")

---
Huge thanks goes to Steven Rostedt for his assistance.

 kernel/trace/blktrace.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/trace/blktrace.c +3 -1
index 4560878f0bac..29ea88f10b87 100644
@@ -1896,8 +1896,10 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
 	}
 
 	ret = 0;
-	if (bt == NULL)
+	if (bt == NULL) {
 		ret = blk_trace_setup_queue(q, bdev);
+		bt = q->blk_trace;
+	}
 
 	if (ret == 0) {
 		if (attr == &dev_attr_act_mask)

--
2.25.1

#1 lore raw
Quoted 26 lines · Cengiz Can

There was a recent change in blktrace.c that added a RCU protection to q->blk_trace in order to fix a use-after-free issue during access.

However the change missed an edge case that can lead to dereferencing of bt pointer even when it's NULL:

```
        bt->act_mask = value; // bt can still be NULL here
```

Added a reassignment into the NULL check block to fix the issue.

Fixes c780e86dd48 ("blktrace: Protect q->blk_trace with RCU")

---
Huge thanks goes to Steven Rostedt for his assistance.

 kernel/trace/blktrace.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/trace/blktrace.c +0 -0
index 4560878f0bac..29ea88f10b87 100644

kernel/trace/blktrace.c static ssize_t sysfs_blk_trace_attr_store(struct device *dev, · line 1901

Quoted 7 lines
kernel/trace/blktrace.c +2 -1
@@ -1896,8 +1896,10 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
 	}
 
 	ret = 0;
-	if (bt == NULL)
+	if (bt == NULL) {
 		ret = blk_trace_setup_queue(q, bdev);
+		bt = q->blk_trace;

The return value 'ret' should be judged, it's wrong to set 'bt' if blk_trace_setup_queue() return failure.

Quoted 7 lines
kernel/trace/blktrace.c +1 -1
+	}
 
 	if (ret == 0) {
 		if (attr == &dev_attr_act_mask)
--

2.25.1

maintainer
#2 lore raw
Quoted 29 lines · On Tue, 3 Mar 2020 21:29:08 +0800 Bob Liu <[email protected]> wrote:
Quoted 28 lines · Cengiz Can

There was a recent change in blktrace.c that added a RCU protection to q->blk_trace in order to fix a use-after-free issue during access.

However the change missed an edge case that can lead to dereferencing of bt pointer even when it's NULL:

```
        bt->act_mask = value; // bt can still be NULL here
```

Added a reassignment into the NULL check block to fix the issue.

Fixes c780e86dd48 ("blktrace: Protect q->blk_trace with RCU")

---
Huge thanks goes to Steven Rostedt for his assistance.

 kernel/trace/blktrace.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/trace/blktrace.c +0 -0
index 4560878f0bac..29ea88f10b87 100644
@@ -1896,8 +1896,10 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
 	}
 
Quoted 8 lines

kernel/trace/blktrace.c static ssize_t sysfs_blk_trace_attr_store(struct device *dev, · line 1901

Quoted 5 lines
kernel/trace/blktrace.c +2 -1
 	ret = 0;
-	if (bt == NULL)
+	if (bt == NULL) {
 		ret = blk_trace_setup_queue(q, bdev);
+		bt = q->blk_trace;  

The return value 'ret' should be judged, it's wrong to set 'bt' if blk_trace_setup_queue() return failure.

Why? If ret is an error, q is still valid, and bt would just be garbage. bt is ignored below if ret is anything but zero. Why add an unnecessary if condition here?

That said, the bt assignment still needs rcu annotation:

		bt = rcu_dereference_protected(q->blk_trace,
				lockdep_is_held(&q->blk_trace_mutex));

-- Steve

Quoted 8 lines
Quoted 8 lines
kernel/trace/blktrace.c +1 -1
+	}
 
 	if (ret == 0) {
 		if (attr == &dev_attr_act_mask)
--

2.25.1

Reviewed-by
#3 lore raw
Quoted 41 lines · Steven Rostedt
Quoted 29 lines · On Tue, 3 Mar 2020 21:29:08 +0800 Bob Liu <[email protected]> wrote:
Quoted 28 lines · Cengiz Can

There was a recent change in blktrace.c that added a RCU protection to q->blk_trace in order to fix a use-after-free issue during access.

However the change missed an edge case that can lead to dereferencing of bt pointer even when it's NULL:

```
        bt->act_mask = value; // bt can still be NULL here
```

Added a reassignment into the NULL check block to fix the issue.

Fixes c780e86dd48 ("blktrace: Protect q->blk_trace with RCU")

---
Huge thanks goes to Steven Rostedt for his assistance.

 kernel/trace/blktrace.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/trace/blktrace.c +0 -0
index 4560878f0bac..29ea88f10b87 100644
@@ -1896,8 +1896,10 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
 	}
 
Quoted 8 lines

kernel/trace/blktrace.c static ssize_t sysfs_blk_trace_attr_store(struct device *dev, · line 1901

Quoted 5 lines
kernel/trace/blktrace.c +2 -1
 	ret = 0;
-	if (bt == NULL)
+	if (bt == NULL) {
 		ret = blk_trace_setup_queue(q, bdev);
+		bt = q->blk_trace;  

The return value 'ret' should be judged, it's wrong to set 'bt' if blk_trace_setup_queue() return failure.

Quoted 4 lines

Why? If ret is an error, q is still valid, and bt would just be garbage. bt is ignored below if ret is anything but zero. Why add an unnecessary if condition here?

Oh..yes. You are right, sorry for missed the if (ret == 0) below.

Quoted 16 lines

That said, the bt assignment still needs rcu annotation:

Quoted 1 line
		bt = rcu_dereference_protected(q->blk_trace,
				lockdep_is_held(&q->blk_trace_mutex));

-- Steve

Quoted 8 lines
Quoted 8 lines
kernel/trace/blktrace.c +1 -1
+	}
 
 	if (ret == 0) {
 		if (attr == &dev_attr_act_mask)
--

2.25.1

maintainer
#4 lore raw
In reply to Cengiz Can
Quoted 11 lines · On Tue, 3 Mar 2020 10:33:59 +0300 Cengiz Can <[email protected]> wrote:

There was a recent change in blktrace.c that added a RCU protection to q->blk_trace in order to fix a use-after-free issue during access.

However the change missed an edge case that can lead to dereferencing of bt pointer even when it's NULL:

```
        bt->act_mask = value; // bt can still be NULL here
```

Added a reassignment into the NULL check block to fix the issue.

Note, you should probably add a note in your change log that this issue was found by Coverity. Sometimes static analyzers have a tag of some kind that they would like patches that fix the issues they discover to be in the change log. This way they can track the fixes that are found by the tool.

Quoted 15 lines
Fixes c780e86dd48 ("blktrace: Protect q->blk_trace with RCU")

---
Huge thanks goes to Steven Rostedt for his assistance.

 kernel/trace/blktrace.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/trace/blktrace.c +0 -0
index 4560878f0bac..29ea88f10b87 100644

kernel/trace/blktrace.c static ssize_t sysfs_blk_trace_attr_store(struct device *dev, · line 1901

Quoted 7 lines
kernel/trace/blktrace.c +2 -1
@@ -1896,8 +1896,10 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
 	}
 
 	ret = 0;
-	if (bt == NULL)
+	if (bt == NULL) {
 		ret = blk_trace_setup_queue(q, bdev);
+		bt = q->blk_trace;

As I said in the other email, the above assignment still needs RCU annotation:

		bt = rcu_dereference_protected(q->blk_trace,
				lockdep_is_held(&q->blk_trace_mutex));

Otherwise, other static analyzers will flag this as a problem.

-- Steve

Quoted 6 lines
kernel/trace/blktrace.c +1 -1
+	}
 
 	if (ret == 0) {
 		if (attr == &dev_attr_act_mask)
--

2.25.1

author
#5 lore raw
Quoted 6 lines · Steven Rostedt

Note, you should probably add a note in your change log that this issue was found by Coverity. Sometimes static analyzers have a tag of some kind that they would like patches that fix the issues they discover to be in the change log. This way they can track the fixes that are found by the tool.

I have added the Coverity issue ID and relevant notes to mark it. Thanks.

Quoted 6 lines

As I said in the other email, the above assignment still needs RCU annotation:

		bt = rcu_dereference_protected(q->blk_trace,
				lockdep_is_held(&q->blk_trace_mutex));

I missed that. Thanks for reminding. TIL.

Please check PATCH-v2.

Signature · 2 lines
Cengiz Can
@cengiz_io