Fix unwanted memory overwrites
Subsystems (2)
Chuck Lever
maintainerv4 cover Fix unwanted memory overwrites
From: Chuck Lever <[email protected]>
<[email protected]> reported some memory overwrites that can be triggered by NFS client input. I was able to observe overwrites by enabling KASAN and running his reproducer [1].
NFSD caches COMPOUNDs containing only a single SEQUENCE operation whether the client requests it to or not, in order to work around a quirk in the NFSv4.1 protocol. However, the predicate that identifies solo SEQUENCE operations was incorrect.
Changes since v3:
* Neil observes that in this code path, SEQUENCE always the first op
* Expanding the size of the replay cache buffer is unnecessary
* Reordered and simplified the remaining patches
* Haven't yet addressed imbalanced maxresponsesize values
Changes since v2:
* Never cache a COMPOUND if SEQUENCE fails
* Enable caching of solo SEQUENCE operations again
* Reserve enough slot replay cache space to cache solo SEQUENCE
Changes since v1:
* Reordered patches
* Disable caching of solo SEQUENCE operations
* Additional clean up
Chuck Lever (4):
NFSD: Skip close replay processing if XDR encoding fails
NFSD: Never cache a COMPOUND when the SEQUENCE operation fails
NFSD: Fix the "is this a solo SEQUENCE" predicate
NFSD: Move nfsd4_cache_this()
fs/nfsd/nfs4state.c | 37 ++++++++++++++++++++++++++++++++++++- fs/nfsd/nfs4xdr.c | 3 +-- fs/nfsd/xdr4.h | 21 --------------------- 3 files changed, 37 insertions(+), 24 deletions(-)
Signature · 1 line
2.51.0
Chuck Lever
maintainerPATCH v4 1/4 NFSD: Skip close replay processing if XDR encoding fails
From: Chuck Lever <[email protected]>
The close replay logic added by commit 9411b1d4c7df ("nfsd4: cleanup handling of nfsv4.0 closed stateid's") cannot be done if encoding failed due to a short send buffer; there's no guarantee that the operation encoder has actually encoded the data that is being copied to the replay cache.
--- fs/nfsd/nfs4xdr.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
fs/nfsd/nfs4xdr.c +1 -2
@@ -5937,8 +5937,7 @@ nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)*/ warn_on_nonidempotent_op (op);xdr_truncate_encode (xdr, op_status_offset +XDR_UNIT ); - } -if (so) { + }else if (so) {int len = xdr->buf->len - (op_status_offset +XDR_UNIT ); so->so_replay.rp_status = op->status;
Signature · 1 line
2.51.0
Chuck Lever
maintainerPATCH v4 2/4 NFSD: Never cache a COMPOUND when the SEQUENCE operation fails
In reply to Chuck LeverFrom: Chuck Lever <[email protected]>
RFC 8881 normatively mandates that operations where the initial SEQUENCE operation in a compound fails must not modify the slot's replay cache.
nfsd4_cache_this() doesn't prevent such caching. So when SEQUENCE fails, cstate.data_offset is not set, allowing read_bytes_from_xdr_buf() to access uninitialized memory.
--- fs/nfsd/nfs4state.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
fs/nfsd/nfs4state.c +14 -1
@@ -3486,7 +3486,20 @@ nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)struct nfsd4_slot *slot = resp->cstate.slot;unsigned int base; -dprintk ("--> %s slot %p\n" , __func__, slot); +/* +* RFC 5661 Section 2.10.6.1.2: +* +* Any time SEQUENCE ... returns an error ... [t]he replier MUST NOT +* modify the reply cache entry for the slot whenever an error is +* returned from SEQUENCE ... +* +* Because nfsd4_store_cache_entry is called only by +* nfsd4_sequence_done(), nfsd4_store_cache_entry() is called only +* when a SEQUENCE operation was part of the COMPOUND. +* nfs41_check_op_ordering() ensures SEQUENCE is the first op. +*/ +if (resp->opcnt ==1 && resp->cstate.status != nfs_ok) +return ; slot->sl_flags |=NFSD4_SLOT_INITIALIZED ; slot->sl_opcnt = resp->opcnt;
Signature · 1 line
2.51.0
Chuck Lever
maintainerPATCH v4 3/4 NFSD: Fix the "is this a solo SEQUENCE" predicate
In reply to Chuck LeverFrom: Chuck Lever <[email protected]>
The logic in nfsd4_is_solo_sequence() is incorrect: it checks the current operation index, not the total count of operations in the COMPOUND. If the SEQUENCE operation, which is always operation 1, fails in a multi-operation compound, resp->opcnt is always 1. Thus when a SEQUENCE operation fails, nfsd4_is_solo_sequence() always returns true.
Note that, because nfsd4_is_solo_sequence() is called only by nfsd4_store_cache_entry(), it is assured that the first operation in the COMPOUND being checked is a SEQUENCE op. Thus the opnum check is redundant.
--- fs/nfsd/xdr4.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
fs/nfsd/xdr4.h +2 -1
@@ -926,7 +926,8 @@ struct nfsd4_compoundres {static inline bool nfsd4_is_solo_sequence (struct nfsd4_compoundres *resp) {struct nfsd4_compoundargs *args = resp->rqstp->rq_argp; -return resp->opcnt ==1 && args->ops[0 ].opnum ==OP_SEQUENCE ; + +return args->opcnt ==1 ; }/*
Signature · 1 line
2.51.0
Chuck Lever
maintainerPATCH v4 4/4 NFSD: Move nfsd4_cache_this()
In reply to Chuck LeverFrom: Chuck Lever <[email protected]>
nfsd4_cache_this() has one call site, and is not related to XDR at all. It doesn't belong in fs/nfsd/xdr4.h.
As a clean-up, move this function (and its helper) to nfs4state.c, next to its only caller.
--- fs/nfsd/nfs4state.c | 22 ++++++++++++++++++++++ fs/nfsd/xdr4.h | 22 ---------------------- 2 files changed, 22 insertions(+), 22 deletions(-)
fs/nfsd/nfs4state.c +22 -0
@@ -3476,6 +3476,28 @@ gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rreturn ; } +static inline bool nfsd4_is_solo_sequence (struct nfsd4_compoundres *resp) +{ +struct nfsd4_compoundargs *args = resp->rqstp->rq_argp; + +return args->opcnt ==1 ; +} + +/* +* The session reply cache only needs to cache replies that the client +* actually asked us to. But it's almost free for us to cache compounds +* consisting of only a SEQUENCE op, so we may as well cache those too. +* Also, the protocol doesn't give us a convenient response in the case +* of a replay of a solo SEQUENCE op that wasn't cached +* (RETRY_UNCACHED_REP can only be returned in the second op of a +* compound). +*/ +static inline bool nfsd4_cache_this (struct nfsd4_compoundres *resp) +{ +return (resp->cstate.slot->sl_flags &NFSD4_SLOT_CACHETHIS ) + ||nfsd4_is_solo_sequence (resp); +} +/* * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. */
fs/nfsd/xdr4.h +0 -22
@@ -923,28 +923,6 @@ struct nfsd4_compoundres {struct nfsd4_compound_state cstate; }; -static inline bool nfsd4_is_solo_sequence (struct nfsd4_compoundres *resp) -{ -struct nfsd4_compoundargs *args = resp->rqstp->rq_argp; - -return args->opcnt ==1 ; -} - -/* -* The session reply cache only needs to cache replies that the client -* actually asked us to. But it's almost free for us to cache compounds -* consisting of only a SEQUENCE op, so we may as well cache those too. -* Also, the protocol doesn't give us a convenient response in the case -* of a replay of a solo SEQUENCE op that wasn't cached -* (RETRY_UNCACHED_REP can only be returned in the second op of a -* compound). -*/ -static inline bool nfsd4_cache_this (struct nfsd4_compoundres *resp) -{ -return (resp->cstate.slot->sl_flags &NFSD4_SLOT_CACHETHIS ) - ||nfsd4_is_solo_sequence (resp); -} -static inline bool nfsd4_last_compound_op (struct svc_rqst *rqstp) {struct nfsd4_compoundres *resp = rqstp->rq_resp;
Signature · 1 line
2.51.0
Re: [PATCH v4 1/4] NFSD: Skip close replay processing if XDR encoding fails
In reply to Chuck LeverQuoted 8 lines · Chuck Lever
From: Chuck Lever <[email protected]>
The close replay logic added by commit
9411b1d4c7df("nfsd4: cleanup handling of nfsv4.0 closed stateid's") cannot be done if encoding failed due to a short send buffer; there's no guarantee that the operation encoder has actually encoded the data that is being copied to the replay cache.
Nit: I don't think this is just for "close" replay. I think it is for reply for any "sequence id mutating" operation that uses an open-owner.
But the patch itself is good.
Thanks,
NeilBrown
Quoted 27 lines
Reported-by <[email protected]>Fixes 9411b1d4c7df ("nfsd4: cleanup handling of nfsv4.0 closed stateid's")Reviewed-by Jeff Layton <[email protected]>Signed-off-by Chuck Lever <[email protected]>--- fs/nfsd/nfs4xdr.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)fs/nfsd/nfs4xdr.c +1
-2@@ -5937,8 +5937,7 @@ nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)*/ warn_on_nonidempotent_op (op);xdr_truncate_encode (xdr, op_status_offset +XDR_UNIT ); - } -if (so) { + }else if (so) {int len = xdr->buf->len - (op_status_offset +XDR_UNIT ); so->so_replay.rp_status = op->status;Signature · 1 line
2.51.0
Re: [PATCH v4 2/4] NFSD: Never cache a COMPOUND when the SEQUENCE operation fails
In reply to Chuck LeverQuoted 6 lines · Chuck Lever
From: Chuck Lever <[email protected]>
RFC 8881 normatively mandates that operations where the initial SEQUENCE operation in a compound fails must not modify the slot's replay cache.
Quoted 8 lines
nfsd4_cache_this()doesn't prevent such caching. So when SEQUENCE fails, cstate.data_offset is not set, allowingread_bytes_from_xdr_buf()to access uninitialized memory.Reported-by <[email protected]>Fixes 468de9e54a90 ("nfsd41: expand solo sequence check")Signed-off-by Chuck Lever <[email protected]>
Thanks,
NeilBrown
Quoted 34 lines
--- fs/nfsd/nfs4state.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)fs/nfsd/nfs4state.c +14
-1@@ -3486,7 +3486,20 @@ nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)struct nfsd4_slot *slot = resp->cstate.slot;unsigned int base; -dprintk ("--> %s slot %p\n" , __func__, slot); +/* +* RFC 5661 Section 2.10.6.1.2: +* +* Any time SEQUENCE ... returns an error ... [t]he replier MUST NOT +* modify the reply cache entry for the slot whenever an error is +* returned from SEQUENCE ... +* +* Because nfsd4_store_cache_entry is called only by +* nfsd4_sequence_done(), nfsd4_store_cache_entry() is called only +* when a SEQUENCE operation was part of the COMPOUND. +* nfs41_check_op_ordering() ensures SEQUENCE is the first op. +*/ +if (resp->opcnt ==1 && resp->cstate.status != nfs_ok) +return ; slot->sl_flags |=NFSD4_SLOT_INITIALIZED ; slot->sl_opcnt = resp->opcnt;Signature · 1 line
2.51.0
Re: [PATCH v4 3/4] NFSD: Fix the "is this a solo SEQUENCE" predicate
In reply to Chuck LeverQuoted 9 lines · Chuck Lever
From: Chuck Lever <[email protected]>
The logic in
nfsd4_is_solo_sequence()is incorrect: it checks the current operation index, not the total count of operations in the COMPOUND. If the SEQUENCE operation, which is always operation 1, fails in a multi-operation compound,resp->opcntis always 1. Thus when a SEQUENCE operation fails,nfsd4_is_solo_sequence()always returns true.
Quoted 4 lines
Note that, because
nfsd4_is_solo_sequence()is called only bynfsd4_store_cache_entry(), it is assured that the first operation in the COMPOUND being checked is a SEQUENCE op. Thus the opnum check is redundant.
It is also assured that the SEQUENCE op didn't fail, so the distinction between resp->opcnt and args->opcnt is moot.
I don't think nfsd4_is_solo_sequence() serves any useful purpose. The only case were the result has any effect, the effect is to set NFSD4_SLOT_CACHED, and to set sl_datalen to zero.
I would prefer that the code didn't pretend that solo sequence requests were cached - they aren't. They are simply performed again when needed. But that can be for another day.
I don't think this patch achieves anything useful, but I don't object to it.
NeilBrowjn
Quoted 25 lines
Reviewed-by Jeff Layton <[email protected]>Signed-off-by Chuck Lever <[email protected]>--- fs/nfsd/xdr4.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)fs/nfsd/xdr4.h +2
-1@@ -926,7 +926,8 @@ struct nfsd4_compoundres {static inline bool nfsd4_is_solo_sequence (struct nfsd4_compoundres *resp) {struct nfsd4_compoundargs *args = resp->rqstp->rq_argp; -return resp->opcnt ==1 && args->ops[0 ].opnum ==OP_SEQUENCE ; + +return args->opcnt ==1 ; }/* Signature · 1 line
2.51.0
Re: [PATCH v4 4/4] NFSD: Move nfsd4_cache_this()
In reply to Chuck LeverQuoted 10 lines · Chuck Lever
From: Chuck Lever <[email protected]>
nfsd4_cache_this()has one call site, and is not related to XDR at all. It doesn't belong infs/nfsd/xdr4.h.As a clean-up, move this function (and its helper) to
nfs4state.c, next to its only caller.Reviewed-by Jeff Layton <[email protected]>Signed-off-by Chuck Lever <[email protected]>
Definitely better having the code local when possible.
Thanks,
NeilBrown
Quoted 75 lines
--- fs/nfsd/nfs4state.c | 22 ++++++++++++++++++++++ fs/nfsd/xdr4.h | 22 ---------------------- 2 files changed, 22 insertions(+), 22 deletions(-)fs/nfsd/nfs4state.c +22
-0@@ -3476,6 +3476,28 @@ gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rreturn ; } +static inline bool nfsd4_is_solo_sequence (struct nfsd4_compoundres *resp) +{ +struct nfsd4_compoundargs *args = resp->rqstp->rq_argp; + +return args->opcnt ==1 ; +} + +/* +* The session reply cache only needs to cache replies that the client +* actually asked us to. But it's almost free for us to cache compounds +* consisting of only a SEQUENCE op, so we may as well cache those too. +* Also, the protocol doesn't give us a convenient response in the case +* of a replay of a solo SEQUENCE op that wasn't cached +* (RETRY_UNCACHED_REP can only be returned in the second op of a +* compound). +*/ +static inline bool nfsd4_cache_this (struct nfsd4_compoundres *resp) +{ +return (resp->cstate.slot->sl_flags &NFSD4_SLOT_CACHETHIS ) + ||nfsd4_is_solo_sequence (resp); +} +/* * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. */ fs/nfsd/xdr4.h +0
-22@@ -923,28 +923,6 @@ struct nfsd4_compoundres {struct nfsd4_compound_state cstate; }; -static inline bool nfsd4_is_solo_sequence (struct nfsd4_compoundres *resp) -{ -struct nfsd4_compoundargs *args = resp->rqstp->rq_argp; - -return args->opcnt ==1 ; -} - -/* -* The session reply cache only needs to cache replies that the client -* actually asked us to. But it's almost free for us to cache compounds -* consisting of only a SEQUENCE op, so we may as well cache those too. -* Also, the protocol doesn't give us a convenient response in the case -* of a replay of a solo SEQUENCE op that wasn't cached -* (RETRY_UNCACHED_REP can only be returned in the second op of a -* compound). -*/ -static inline bool nfsd4_cache_this (struct nfsd4_compoundres *resp) -{ -return (resp->cstate.slot->sl_flags &NFSD4_SLOT_CACHETHIS ) - ||nfsd4_is_solo_sequence (resp); -} -static inline bool nfsd4_last_compound_op (struct svc_rqst *rqstp) {struct nfsd4_compoundres *resp = rqstp->rq_resp;Signature · 1 line
2.51.0
Chuck Lever
maintainerRe: [PATCH v4 3/4] NFSD: Fix the "is this a solo SEQUENCE" predicate
In reply to NeilBrownQuoted 18 lines · NeilBrown
Quoted 9 lines · Chuck Lever
From: Chuck Lever <[email protected]>
The logic in
nfsd4_is_solo_sequence()is incorrect: it checks the current operation index, not the total count of operations in the COMPOUND. If the SEQUENCE operation, which is always operation 1, fails in a multi-operation compound,resp->opcntis always 1. Thus when a SEQUENCE operation fails,nfsd4_is_solo_sequence()always returns true.Quoted 4 lines
Note that, because
nfsd4_is_solo_sequence()is called only bynfsd4_store_cache_entry(), it is assured that the first operation in the COMPOUND being checked is a SEQUENCE op. Thus the opnum check is redundant.It is also assured that the SEQUENCE op didn't fail, so the distinction between
resp->opcntandargs->opcntis moot.
Quoted 7 lines
I don't think
nfsd4_is_solo_sequence()serves any useful purpose. The only case were the result has any effect, the effect is to setNFSD4_SLOT_CACHED, and to setsl_datalento zero.I would prefer that the code didn't pretend that solo sequence requests were cached - they aren't. They are simply performed again when needed. But that can be for another day.
One wonders why the comments take such pains to call out "caching solo sequence" operations if these operations aren't actually cached. Perhaps I should replace this patch with one that removes nfsd4_is_solo_sequence() .
Here's why this is relevant:
941 static inline bool nfsd4_cache_this(struct nfsd4_compoundres *resp)
942 {
943 return (resp->cstate.slot->sl_flags & NFSD4_SLOT_CACHETHIS)
944 || nfsd4_is_solo_sequence(resp);
945 }
If nfsd4_is_solo_sequence always returns true, then so does nfsd4_cache_this, it looks like.
Quoted 33 lines
I don't think this patch achieves anything useful, but I don't object to it.
NeilBrowjn
Quoted 25 lines
Reviewed-by Jeff Layton <[email protected]>Signed-off-by Chuck Lever <[email protected]>--- fs/nfsd/xdr4.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)fs/nfsd/xdr4.h +2
-1@@ -926,7 +926,8 @@ struct nfsd4_compoundres {static inline bool nfsd4_is_solo_sequence (struct nfsd4_compoundres *resp) {struct nfsd4_compoundargs *args = resp->rqstp->rq_argp; -return resp->opcnt ==1 && args->ops[0 ].opnum ==OP_SEQUENCE ; + +return args->opcnt ==1 ; }/* Signature · 1 line
2.51.0
Signature · 1 line
Chuck Lever
Re: [PATCH v4 3/4] NFSD: Fix the "is this a solo SEQUENCE" predicate
Quoted 38 lines · Chuck Lever
Quoted 18 lines · NeilBrown
Quoted 9 lines · Chuck Lever
From: Chuck Lever <[email protected]>
The logic in
nfsd4_is_solo_sequence()is incorrect: it checks the current operation index, not the total count of operations in the COMPOUND. If the SEQUENCE operation, which is always operation 1, fails in a multi-operation compound,resp->opcntis always 1. Thus when a SEQUENCE operation fails,nfsd4_is_solo_sequence()always returns true.Quoted 4 lines
Note that, because
nfsd4_is_solo_sequence()is called only bynfsd4_store_cache_entry(), it is assured that the first operation in the COMPOUND being checked is a SEQUENCE op. Thus the opnum check is redundant.It is also assured that the SEQUENCE op didn't fail, so the distinction between
resp->opcntandargs->opcntis moot.Quoted 7 lines
I don't think
nfsd4_is_solo_sequence()serves any useful purpose. The only case were the result has any effect, the effect is to setNFSD4_SLOT_CACHED, and to setsl_datalento zero.I would prefer that the code didn't pretend that solo sequence requests were cached - they aren't. They are simply performed again when needed. But that can be for another day.
One wonders why the comments take such pains to call out "caching solo sequence" operations if these operations aren't actually cached. Perhaps I should replace this patch with one that removes
nfsd4_is_solo_sequence().Here's why this is relevant:
941 static inline bool
nfsd4_cache_this(struct nfsd4_compoundres *resp)942 {
Quoted 8 lines
943 return (
resp->cstate.slot->sl_flags&NFSD4_SLOT_CACHETHIS)944 ||
nfsd4_is_solo_sequence(resp);945 }
If
nfsd4_is_solo_sequencealways returns true, then so doesnfsd4_cache_this, it looks like.
True, but nfsd4_is_solo_sequence() doesn't always return true. If any op before the SEQUENCE has been attempted, then it will fail.
But yes, lets remove nfsd4_is_solo_sequence(). I'll send a couple of patches to show what I am thinking.
NeilBrown
Quoted 40 lines
Quoted 35 lines
I don't think this patch achieves anything useful, but I don't object to it.
NeilBrowjn
Quoted 25 lines
Reviewed-by Jeff Layton <[email protected]>Signed-off-by Chuck Lever <[email protected]>--- fs/nfsd/xdr4.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)fs/nfsd/xdr4.h +2
-1@@ -926,7 +926,8 @@ struct nfsd4_compoundres {static inline bool nfsd4_is_solo_sequence (struct nfsd4_compoundres *resp) {struct nfsd4_compoundargs *args = resp->rqstp->rq_argp; -return resp->opcnt ==1 && args->ops[0 ].opnum ==OP_SEQUENCE ; + +return args->opcnt ==1 ; }/* Signature · 1 line
2.51.0Signature · 1 line
Chuck Lever