fuse: prevent possible NULL pointer dereference in fuse_iomap_writeback_{range,submit}()
Subsystems (2)
PATCH v2 1/1 fuse: prevent possible NULL pointer dereference in fuse_iomap_writeback_{range,submit}()
These two functions make use of the WARN_ON_ONCE() macro to help debugging a NULL wpc->wb_ctx. However, this doesn't prevent the possibility of NULL pointer dereferences in the code. This patch adds some extra defensive checks to avoid these NULL pointer accesses.
---
Hi!
This v2 results from Joanne's inputs -- I now believe that it is better to keep the WARN_ON_ONCE() macros, but it's still good to try to minimise the undesirable effects of a NULL wpc->wb_ctx.
I've also added the 'Fixes:' tag to the commit message.
fs/fuse/file.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
fs/fuse/file.c +9 -4
@@ -2135,14 +2135,18 @@ static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc, unsigned len, u64 end_pos) {struct fuse_fill_wb_data *data = wpc->wb_ctx; -struct fuse_writepage_args *wpa = data->wpa; -struct fuse_args_pages *ap = &wpa->ia.ap; +struct fuse_writepage_args *wpa; +struct fuse_args_pages *ap;struct inode *inode = wpc->inode;struct fuse_inode *fi =get_fuse_inode (inode);struct fuse_conn *fc =get_fuse_conn (inode);loff_t offset =offset_in_folio (folio, pos); -WARN_ON_ONCE (!data); +if (WARN_ON_ONCE (!data)) +return -EIO ; + + wpa = data->wpa; + ap = &wpa->ia.ap;if (!data->ff) { data->ff =fuse_write_file_get (fi);
@@ -2182,7 +2186,8 @@ static int fuse_iomap_writeback_submit(struct iomap_writepage_ctx *wpc, {struct fuse_fill_wb_data *data = wpc->wb_ctx; -WARN_ON_ONCE (!data); +if (WARN_ON_ONCE (!data)) +return error ? error : -EIO ;if (data->wpa) {WARN_ON (!data->wpa->ia.ap.num_folios);
Quoted 30 lines · Luis Henriques
These two functions make use of the
WARN_ON_ONCE()macro to help debugging a NULLwpc->wb_ctx. However, this doesn't prevent the possibility of NULL pointer dereferences in the code. This patch adds some extra defensive checks to avoid these NULL pointer accesses.Fixes ef7e7cbb323f ("fuse: use iomap for writeback")Signed-off-by Luis Henriques <[email protected]>---
Hi!This v2 results from Joanne's inputs -- I now believe that it is better to keep the
WARN_ON_ONCE()macros, but it's still good to try to minimise the undesirable effects of a NULLwpc->wb_ctx.I've also added the 'Fixes:' tag to the commit message.
fs/fuse/file.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)fs/fuse/file.c +0
-2@@ -2135,14 +2135,18 @@ static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc, unsigned len, u64 end_pos) {struct fuse_fill_wb_data *data = wpc->wb_ctx; -struct fuse_writepage_args *wpa = data->wpa; -struct fuse_args_pages *ap = &wpa->ia.ap;
fs/fuse/file.c static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc, · line 2146
Quoted 10 lines
fs/fuse/file.c +4
-1+struct fuse_writepage_args *wpa; +struct fuse_args_pages *ap;struct inode *inode = wpc->inode;struct fuse_inode *fi =get_fuse_inode (inode);struct fuse_conn *fc =get_fuse_conn (inode);loff_t offset =offset_in_folio (folio, pos); -WARN_ON_ONCE (!data); +if (WARN_ON_ONCE (!data)) +return -EIO ;
imo this WARN_ON_ONCE (and the one below) should be left as is instead of embedded in the "if" construct. The data pointer passed in is set by fuse and as such, we're able to reasonably guarantee that data is a valid pointer. Looking at other examples of WARN_ON in the fuse codebase, the places where an "if" construct is used are for cases where the assumptions that are made are more delicate (eg folio mapping state, in fuse_try_move_folio()) and less clearly obvious. I think this WARN_ON_ONCE here and below should be left as is.
Thanks,
Joanne
Quoted 16 lines
fs/fuse/file.c +5
-1+ + wpa = data->wpa; + ap = &wpa->ia.ap;if (!data->ff) { data->ff =fuse_write_file_get (fi);@@ -2182,7 +2186,8 @@ static int fuse_iomap_writeback_submit(struct iomap_writepage_ctx *wpc, {struct fuse_fill_wb_data *data = wpc->wb_ctx; -WARN_ON_ONCE (!data); +if (WARN_ON_ONCE (!data)) +return error ? error : -EIO ;if (data->wpa) {WARN_ON (!data->wpa->ia.ap.num_folios);
Quoted 38 lines · Joanne Koong
Quoted 37 lines · Luis Henriques
These two functions make use of the
WARN_ON_ONCE()macro to help debugging a NULLwpc->wb_ctx. However, this doesn't prevent the possibility of NULL pointer dereferences in the code. This patch adds some extra defensive checks to avoid these NULL pointer accesses.Fixes ef7e7cbb323f ("fuse: use iomap for writeback")Signed-off-by Luis Henriques <[email protected]>---
Hi!This v2 results from Joanne's inputs -- I now believe that it is better to keep the
WARN_ON_ONCE()macros, but it's still good to try to minimise the undesirable effects of a NULLwpc->wb_ctx.I've also added the 'Fixes:' tag to the commit message.
fs/fuse/file.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)fs/fuse/file.c +2
-2@@ -2135,14 +2135,18 @@ static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc, unsigned len, u64 end_pos) {struct fuse_fill_wb_data *data = wpc->wb_ctx; -struct fuse_writepage_args *wpa = data->wpa; -struct fuse_args_pages *ap = &wpa->ia.ap; +struct fuse_writepage_args *wpa; +struct fuse_args_pages *ap;struct inode *inode = wpc->inode;struct fuse_inode *fi =get_fuse_inode (inode);struct fuse_conn *fc =get_fuse_conn (inode);loff_t offset =offset_in_folio (folio, pos);
Quoted 12 lines
fs/fuse/file.c static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc, · line 2146
Quoted 3 lines
fs/fuse/file.c +2
-1-WARN_ON_ONCE (!data); +if (WARN_ON_ONCE (!data)) +return -EIO ;imo this
WARN_ON_ONCE(and the one below) should be left as is instead of embedded in the "if" construct. The data pointer passed in is set by fuse and as such, we're able to reasonably guarantee that data is a valid pointer. Looking at other examples ofWARN_ONin the fuse codebase, the places where an "if" construct is used are for cases where the assumptions that are made are more delicate (eg folio mapping state, infuse_try_move_folio()) and less clearly obvious. I think thisWARN_ON_ONCEhere and below should be left as is.
OK, thank you for your feedback, Joanne. So, if Miklos agrees with that, I guess we can drop this patch.
Cheers,
Signature · 1 line
Luís
Quoted 21 lines
Thanks,
JoanneQuoted 16 lines
fs/fuse/file.c +5
-1+ + wpa = data->wpa; + ap = &wpa->ia.ap;if (!data->ff) { data->ff =fuse_write_file_get (fi);@@ -2182,7 +2186,8 @@ static int fuse_iomap_writeback_submit(struct iomap_writepage_ctx *wpc, {struct fuse_fill_wb_data *data = wpc->wb_ctx; -WARN_ON_ONCE (!data); +if (WARN_ON_ONCE (!data)) +return error ? error : -EIO ;if (data->wpa) {WARN_ON (!data->wpa->ia.ap.num_folios);
Quoted 53 lines · Luis Henriques
Quoted 38 lines · Joanne Koong
Quoted 37 lines · Luis Henriques
These two functions make use of the
WARN_ON_ONCE()macro to help debugging a NULLwpc->wb_ctx. However, this doesn't prevent the possibility of NULL pointer dereferences in the code. This patch adds some extra defensive checks to avoid these NULL pointer accesses.Fixes ef7e7cbb323f ("fuse: use iomap for writeback")Signed-off-by Luis Henriques <[email protected]>---
Hi!This v2 results from Joanne's inputs -- I now believe that it is better to keep the
WARN_ON_ONCE()macros, but it's still good to try to minimise the undesirable effects of a NULLwpc->wb_ctx.I've also added the 'Fixes:' tag to the commit message.
fs/fuse/file.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)fs/fuse/file.c +2
-2@@ -2135,14 +2135,18 @@ static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc, unsigned len, u64 end_pos) {struct fuse_fill_wb_data *data = wpc->wb_ctx; -struct fuse_writepage_args *wpa = data->wpa; -struct fuse_args_pages *ap = &wpa->ia.ap; +struct fuse_writepage_args *wpa; +struct fuse_args_pages *ap;struct inode *inode = wpc->inode;struct fuse_inode *fi =get_fuse_inode (inode);struct fuse_conn *fc =get_fuse_conn (inode);loff_t offset =offset_in_folio (folio, pos);Quoted 12 lines
fs/fuse/file.c static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc, · line 2146
Quoted 3 lines
fs/fuse/file.c +2
-1-WARN_ON_ONCE (!data); +if (WARN_ON_ONCE (!data)) +return -EIO ;imo this
WARN_ON_ONCE(and the one below) should be left as is instead of embedded in the "if" construct. The data pointer passed in is set by fuse and as such, we're able to reasonably guarantee that data is a valid pointer. Looking at other examples ofWARN_ONin the fuse codebase, the places where an "if" construct is used are for cases where the assumptions that are made are more delicate (eg folio mapping state, infuse_try_move_folio()) and less clearly obvious. I think thisWARN_ON_ONCEhere and below should be left as is.
Quoted 2 lines
OK, thank you for your feedback, Joanne. So, if Miklos agrees with that, I guess we can drop this patch.
AFAICT, this function can only be called by other iomap-using functions in file.c, and those other functions always set iomap_writepage_ctx::wb_ctx so I /think/ the assertions aren't necessary at all...
Quoted 14 lines
Cheers,
Signature · 1 line
LuísQuoted 10 lines
Thanks,
JoanneQuoted 5 lines
fs/fuse/file.c +3
-0+ + wpa = data->wpa; + ap = &wpa->ia.ap;if (!data->ff) {
...because if someone fails to set wpc->wb_ctx, this line will crash the kernel at least as much as the WARN_ON would. IOWs, the WARN_ONs aren't necessary but I don't think they hurt much.
You could introduce a CONFIG_FUSE_DEBUG option and hide some assertions and whatnot behind it, ala CONFIG_FUSE_IOMAP_DEBUG*:
--D
Quoted 13 lines
Quoted 11 lines
Quoted 11 lines
fs/fuse/file.c +2
-1data->ff =fuse_write_file_get (fi);@@ -2182,7 +2186,8 @@ static int fuse_iomap_writeback_submit(struct iomap_writepage_ctx *wpc, {struct fuse_fill_wb_data *data = wpc->wb_ctx; -WARN_ON_ONCE (!data); +if (WARN_ON_ONCE (!data)) +return error ? error : -EIO ;if (data->wpa) {WARN_ON (!data->wpa->ia.ap.num_folios);
Quoted 55 lines · Darrick J. Wong
Quoted 53 lines · Luis Henriques
Quoted 50 lines · Joanne Koong
Quoted 30 lines · Luis Henriques
These two functions make use of the
WARN_ON_ONCE()macro to help debugging a NULLwpc->wb_ctx. However, this doesn't prevent the possibility of NULL pointer dereferences in the code. This patch adds some extra defensive checks to avoid these NULL pointer accesses.Fixes ef7e7cbb323f ("fuse: use iomap for writeback")Signed-off-by Luis Henriques <[email protected]>---
Hi!This v2 results from Joanne's inputs -- I now believe that it is better to keep the
WARN_ON_ONCE()macros, but it's still good to try to minimise the undesirable effects of a NULLwpc->wb_ctx.I've also added the 'Fixes:' tag to the commit message.
fs/fuse/file.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)fs/fuse/file.c +0
-2@@ -2135,14 +2135,18 @@ static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc, unsigned len, u64 end_pos) {struct fuse_fill_wb_data *data = wpc->wb_ctx; -struct fuse_writepage_args *wpa = data->wpa; -struct fuse_args_pages *ap = &wpa->ia.ap;fs/fuse/file.c static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc, · line 2146
Quoted 10 lines
fs/fuse/file.c +4
-1+struct fuse_writepage_args *wpa; +struct fuse_args_pages *ap;struct inode *inode = wpc->inode;struct fuse_inode *fi =get_fuse_inode (inode);struct fuse_conn *fc =get_fuse_conn (inode);loff_t offset =offset_in_folio (folio, pos); -WARN_ON_ONCE (!data); +if (WARN_ON_ONCE (!data)) +return -EIO ;imo this
WARN_ON_ONCE(and the one below) should be left as is instead of embedded in the "if" construct. The data pointer passed in is set by fuse and as such, we're able to reasonably guarantee that data is a valid pointer. Looking at other examples ofWARN_ONin the fuse codebase, the places where an "if" construct is used are for cases where the assumptions that are made are more delicate (eg folio mapping state, infuse_try_move_folio()) and less clearly obvious. I think thisWARN_ON_ONCEhere and below should be left as is.
Quoted 2 lines
Quoted 2 lines
OK, thank you for your feedback, Joanne. So, if Miklos agrees with that, I guess we can drop this patch.
I think having the two lines "wpa = data->wpa;" and "ap = &wpa->ia.ap" moved to below the "WARN_ON_ONCE(!data);" would still be useful
Quoted 19 lines
AFAICT, this function can only be called by other iomap-using functions in
file.c, and those other functions always setiomap_writepage_ctx::wb_ctxso I /think/ the assertions aren't necessary at all...Quoted 13 lines
Cheers,
--
LuísQuoted 9 lines
Thanks,
JoanneQuoted 4 lines
fs/fuse/file.c +3
-0+ + wpa = data->wpa; + ap = &wpa->ia.ap;
Quoted 6 lines
Quoted 1 line
Quoted 1 line
fs/fuse/file.c static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc, · line 2151
Quoted 1 line
fs/fuse/file.c +0
-0if (!data->ff) {...because if someone fails to set
wpc->wb_ctx, this line will crash the kernel at least as much as theWARN_ONwould. IOWs, theWARN_ONsaren't necessary but I don't think they hurt much.
Oh, I see. Actually, this explanation makes a lot of sense. When I was looking at the other WARN_ON usages in fuse, I noticed they were also used even if it's logically proven that the code path can never be triggered. But I guess what you're saying is that WARN_ONs in general should be used if it's otherwise somehow undetectable / non-obvious that the condition is violated? That makes sense to me, and checks out with the other fuse WARN_ON uses.
I'm fine with just removing the WARN_ON(!data) here and below. I think I added some more WARN_ONs in my other fuse iomap patchset, so I'll remove those as well when I send out a new version.
Quoted 5 lines
You could introduce a
CONFIG_FUSE_DEBUGoption and hide some assertions and whatnot behind it, ala CONFIG_FUSE_IOMAP_DEBUG*:
In that case, personally I'd much prefer removing the WARN_ONs here than having a new config for it.
Thanks,
Joanne
Quoted 15 lines
--D
Quoted 13 lines
Quoted 11 lines
Quoted 11 lines
fs/fuse/file.c +2
-1data->ff =fuse_write_file_get (fi);@@ -2182,7 +2186,8 @@ static int fuse_iomap_writeback_submit(struct iomap_writepage_ctx *wpc, {struct fuse_fill_wb_data *data = wpc->wb_ctx; -WARN_ON_ONCE (!data); +if (WARN_ON_ONCE (!data)) +return error ? error : -EIO ;if (data->wpa) {WARN_ON (!data->wpa->ia.ap.num_folios);
Quoted 56 lines · Joanne Koong
Quoted 55 lines · Darrick J. Wong
Quoted 53 lines · Luis Henriques
Quoted 50 lines · Joanne Koong
Quoted 30 lines · Luis Henriques
These two functions make use of the
WARN_ON_ONCE()macro to help debugging a NULLwpc->wb_ctx. However, this doesn't prevent the possibility of NULL pointer dereferences in the code. This patch adds some extra defensive checks to avoid these NULL pointer accesses.Fixes ef7e7cbb323f ("fuse: use iomap for writeback")Signed-off-by Luis Henriques <[email protected]>---
Hi!This v2 results from Joanne's inputs -- I now believe that it is better to keep the
WARN_ON_ONCE()macros, but it's still good to try to minimise the undesirable effects of a NULLwpc->wb_ctx.I've also added the 'Fixes:' tag to the commit message.
fs/fuse/file.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)fs/fuse/file.c +0
-2@@ -2135,14 +2135,18 @@ static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc, unsigned len, u64 end_pos) {struct fuse_fill_wb_data *data = wpc->wb_ctx; -struct fuse_writepage_args *wpa = data->wpa; -struct fuse_args_pages *ap = &wpa->ia.ap;fs/fuse/file.c static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc, · line 2146
Quoted 10 lines
fs/fuse/file.c +4
-1+struct fuse_writepage_args *wpa; +struct fuse_args_pages *ap;struct inode *inode = wpc->inode;struct fuse_inode *fi =get_fuse_inode (inode);struct fuse_conn *fc =get_fuse_conn (inode);loff_t offset =offset_in_folio (folio, pos); -WARN_ON_ONCE (!data); +if (WARN_ON_ONCE (!data)) +return -EIO ;imo this
WARN_ON_ONCE(and the one below) should be left as is instead of embedded in the "if" construct. The data pointer passed in is set by fuse and as such, we're able to reasonably guarantee that data is a valid pointer. Looking at other examples ofWARN_ONin the fuse codebase, the places where an "if" construct is used are for cases where the assumptions that are made are more delicate (eg folio mapping state, infuse_try_move_folio()) and less clearly obvious. I think thisWARN_ON_ONCEhere and below should be left as is.
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
OK, thank you for your feedback, Joanne. So, if Miklos agrees with that, I guess we can drop this patch.
I think having the two lines "wpa =
data->wpa;" and "ap =&wpa->ia.ap" moved to below the "WARN_ON_ONCE(!data);" would still be useful
<shrug> Compilers are magic, they can rearrange the function unless you explicitly put in barriers or data dependencies to prevent that. 8-)
Quoted 34 lines
Quoted 19 lines
AFAICT, this function can only be called by other iomap-using functions in
file.c, and those other functions always setiomap_writepage_ctx::wb_ctxso I /think/ the assertions aren't necessary at all...Quoted 13 lines
Cheers,
--
LuísQuoted 9 lines
Thanks,
JoanneQuoted 4 lines
fs/fuse/file.c +3
-0+ + wpa = data->wpa; + ap = &wpa->ia.ap;Quoted 6 lines
Quoted 1 line
Quoted 1 line
fs/fuse/file.c static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc, · line 2151
Quoted 1 line
fs/fuse/file.c +0
-0if (!data->ff) {...because if someone fails to set
wpc->wb_ctx, this line will crash the kernel at least as much as theWARN_ONwould. IOWs, theWARN_ONsaren't necessary but I don't think they hurt much.Oh, I see. Actually, this explanation makes a lot of sense. When I was looking at the other
WARN_ONusages in fuse, I noticed they were also used even if it's logically proven that the code path can never be triggered. But I guess what you're saying is thatWARN_ONsin general should be used if it's otherwise somehow undetectable / non-obvious that the condition is violated? That makes sense to me, and checks out with the other fuseWARN_ONuses.
Quoted 3 lines
I'm fine with just removing the
WARN_ON(!data)here and below. I think I added some moreWARN_ONsin my other fuse iomap patchset, so I'll remove those as well when I send out a new version.
<nod>
Quoted 8 lines
Quoted 5 lines
You could introduce a
CONFIG_FUSE_DEBUGoption and hide some assertions and whatnot behind it, ala CONFIG_FUSE_IOMAP_DEBUG*:In that case, personally I'd much prefer removing the
WARN_ONshere than having a new config for it.
<nod> I added it to mine because there are a lot of things that iomap /can/ get cranky about, so it's useful to have a "BAD_DATA" macro that screams when the fuse server feeds garbage to the kernel.
--D
Quoted 18 lines
Thanks,
JoanneQuoted 15 lines
--D
Quoted 13 lines
Quoted 11 lines
Quoted 11 lines
fs/fuse/file.c +2
-1data->ff =fuse_write_file_get (fi);@@ -2182,7 +2186,8 @@ static int fuse_iomap_writeback_submit(struct iomap_writepage_ctx *wpc, {struct fuse_fill_wb_data *data = wpc->wb_ctx; -WARN_ON_ONCE (!data); +if (WARN_ON_ONCE (!data)) +return error ? error : -EIO ;if (data->wpa) {WARN_ON (!data->wpa->ia.ap.num_folios);
Quoted 15 lines · Joanne Koong
Quoted 4 lines · Darrick J. Wong
...because if someone fails to set
wpc->wb_ctx, this line will crash the kernel at least as much as theWARN_ONwould. IOWs, theWARN_ONsaren't necessary but I don't think they hurt much.Oh, I see. Actually, this explanation makes a lot of sense. When I was looking at the other
WARN_ONusages in fuse, I noticed they were also used even if it's logically proven that the code path can never be triggered. But I guess what you're saying is thatWARN_ONsin general should be used if it's otherwise somehow undetectable / non-obvious that the condition is violated? That makes sense to me, and checks out with the other fuseWARN_ONuses.
Quoted 3 lines
I'm fine with just removing the
WARN_ON(!data)here and below. I think I added some moreWARN_ONsin my other fuse iomap patchset, so I'll remove those as well when I send out a new version.
I don't have a preference between v1 and v2 of this patch. v1 removed the WARNs because I don't think they are useful:
1. the assertions are never true, but
2. if they are, they are useless because we'll hit a NULL pointer dereference anyway.
v2 tries to fix the code assuming the assertions can be triggered.
So, yeah I'll just leave the 3 options (v1, v2, or do nothing) on the table :-)
Cheers,
Signature · 1 line
Luís
Miklos Szeredi
maintainerQuoted 11 lines · Luis Henriques
I don't have a preference between v1 and v2 of this patch. v1 removed the WARNs because I don't think they are useful:
1. the assertions are never true, but
2. if they are, they are useless because we'll hit a NULL pointer dereference anyway.v2 tries to fix the code assuming the assertions can be triggered.
So, yeah I'll just leave the 3 options (v1, v2, or do nothing) on the table :-)
WARN_ON is a useful tool to document interface constrains. But so is dereferencing of a pointer.
V2 style WARN_ON should only be used if it's difficult to prove that the condition will evaluate to false and we don't want the kernel to crash in some unknown corner case. AFAICS it is not the case here, so I'd opt for v1.
Thanks,
Miklos