nfsd: reject out-of-range useconds in NFSv2 SETATTR/CREATE
Subsystems (2)
PATCH v3 1/3 nfsd: reject out-of-range useconds in NFSv2 SETATTR/CREATE
From: Robbie Ko <[email protected]>
The NFSv2 sattr decoder converts the wire useconds to nanoseconds in svcxdr_decode_sattr():
iap->ia_atime.tv_nsec = tmp2 * NSEC_PER_USEC;
tmp2 is a u32 and NSEC_PER_USEC is 1000, so the product is computed in unsigned long. On ILP32 that is 32 bits, and an out-of-range useconds value such as 4294968 wraps to tv_nsec == 704. The corruption therefore happens during decode, before any proc function can inspect the value, and a later range check on tv_nsec would see an in-range result and accept it.
Guard the raw useconds before the multiplication and reject values greater than 1000000. useconds == 1000000 is kept: it is the Sun convention for "set to the current server time", and the in-tree Linux NFSv2 client emits it in both the atime and the mtime field for a plain touch / utimes(file, NULL) (see encode_sattr() and xdr_encode_current_server_time() in fs/nfs/nfs2xdr.c). Rejecting 1000000 would turn that common operation into a hard decode failure for both SETATTR and CREATE. 1000000 * NSEC_PER_USEC is 10^9, which does not wrap on ILP32, so the Sun convention value passes through safely; only genuinely out-of-range values (> 1000000) are rejected. The atime and mtime guards are therefore symmetric.
The decoder only applied the Sun convention in the mtime block, which clears ATTR_ATIME_SET|ATTR_MTIME_SET when mtime useconds == 1000000. If a client puts 1000000 in the atime field but not in the mtime field, the atime block stored an out-of-range tv_nsec (10^9) and left ATTR_ATIME_SET set, so the bogus value reached the filesystem. Apply the convention in the atime block as well, clearing ATTR_ATIME_SET so the server uses its current time and ignores the value. Only ATTR_ATIME_SET is cleared there; the mtime block keeps its existing behavior, where 1000000 means "set both atime and mtime to now".
--- fs/nfsd/nfsxdr.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
fs/nfsd/nfsxdr.c +31 -0
@@ -172,14 +172,45 @@ svcxdr_decode_sattr(struct svc_rqst *rqstp, struct xdr_stream *xdr, tmp1 =be32_to_cpup (p++); tmp2 =be32_to_cpup (p++);if (tmp1 != (u32)-1 && tmp2 != (u32)-1 ) { +/* +* Guard the raw useconds before the unit conversion below. +* tmp2 * NSEC_PER_USEC is computed in unsigned long, which is +* 32 bits on ILP32, so an out-of-range value would wrap and +* silently produce a bogus in-range tv_nsec. useconds == +* 1000000 is the Sun "set to current server time" convention +* (see the mtime block below); allow it and reject anything +* larger. Note 1000000 * NSEC_PER_USEC is 10^9, which does not +* wrap on ILP32. +*/ +if (tmp2 >1000000 ) +return false; iap->ia_valid |=ATTR_ATIME |ATTR_ATIME_SET ; iap->ia_atime.tv_sec = tmp1; iap->ia_atime.tv_nsec = tmp2 *NSEC_PER_USEC ; +/* +* The Linux NFSv2 client emits useconds == 1000000 in the +* atime field too (touch / utimes(file, NULL) sets ATTR_ATIME +* without ATTR_ATIME_SET). Apply the Sun convention here so +* the server uses its current time and ignores the bogus +* tv_nsec, instead of storing an out-of-range value when the +* mtime field does not also carry 1000000. Only ATTR_ATIME_SET +* is cleared; the mtime block keeps its own handling, where +* 1000000 means "set both atime and mtime to now". +*/ +if (tmp2 ==1000000 ) + iap->ia_valid &= ~ATTR_ATIME_SET ; } tmp1 =be32_to_cpup (p++); tmp2 =be32_to_cpup (p++);if (tmp1 != (u32)-1 && tmp2 != (u32)-1 ) { +/* +* useconds == 1000000 is a valid Sun convention here (see +* below); anything above that is out of range. Guard it before +* the unit conversion to avoid the ILP32 wrap described above. +*/ +if (tmp2 >1000000 ) +return false; iap->ia_valid |=ATTR_MTIME |ATTR_MTIME_SET ; iap->ia_mtime.tv_sec = tmp1; iap->ia_mtime.tv_nsec = tmp2 *NSEC_PER_USEC ;
Signature · 4 lines
2.43.0 Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.
PATCH v3 2/3 nfsd: reject out-of-range nseconds in NFSv3 SETATTR and create ops
From: Robbie Ko <[email protected]>
A client can send an NFSv3 SETATTR, CREATE, MKDIR, SYMLINK or MKNOD carrying an atime or mtime whose nseconds field is out of range. The value is well-formed on the wire and decodes cleanly into a valid uint32, but it is not a valid timespec64: tv_nsec must be less than NSEC_PER_SEC.
Nothing in the setattr path clamps it. notify_change() runs the time through timestamp_truncate(), which does not reduce tv_nsec below NSEC_PER_SEC when the filesystem supports nanosecond granularity (s_time_gran == 1), and the inode atime/mtime setters store it verbatim (only ctime is normalized, via inode_set_ctime_to_ts()). The un-normalized value then corrupts on-disk metadata: ext4's ext4_encode_extra_time() shifts tv_nsec left by EXT4_EPOCH_BITS, which overflows the 32-bit extra field and clobbers the seconds-epoch bits, so the stored seconds (and thus the year) are wrong on read-back. XFS with bigtime mis-stores the timestamp for the same reason.
Validate the client-supplied atime/mtime in the proc handlers and return NFS3ERR_INVAL before anything is changed. RFC 1813 lists NFS3ERR_INVAL for SETATTR and describes it as the error for a value the server 'can not store ... in its own representation'; the client maps it to EINVAL.
Checking in the proc handlers, rather than in nfsd_setattr(), keeps the rejection in front of object creation. The create operations create the object before nfsd_create_setattr() runs, so a late failure would leave the new object behind and turn a non-idempotent request into a namespace change that reports failure. The check is therefore done up front, for the create operations before the object is created.
tv_nsec is a long, so the comparison casts it to unsigned long (the same width) rather than to u32, matching timespec64_valid(). A u32 cast would truncate on 64-bit; the unsigned long cast also rejects a value that became negative when an out-of-range u32 wire nseconds was assigned to a 32-bit long.
Only client-supplied times are checked: SET_TO_SERVER_TIME requests carry no client value. The sattrguard3 ctime is deliberately left alone:
an out-of-range guard simply never matches the object's ctime and yields NFS3ERR_NOT_SYNC via the existing guardtime comparison, which is the protocol-correct outcome rather than rejecting the request.
--- fs/nfsd/nfs3proc.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+)
fs/nfsd/nfs3proc.c +40 -0
@@ -29,6 +29,25 @@ static int nfs3_ftypes[] = { S_IFIFO, /* NF3FIFO */ }; +/* +* Reject a client-supplied atime or mtime whose nanoseconds field is out +* of range. Such a value is well-formed on the wire but is not a valid +* timespec64, and storing it verbatim can corrupt on-disk timestamps. +* tv_nsec is a long, so it is cast to unsigned long (the same width) to +* catch both an over-large value and one that became negative when an +* out-of-range u32 wire nseconds was assigned to a 32-bit long. +*/ +static bool nfsd3_time_in_range (const struct iattr *iap) +{ +if ((iap->ia_valid &ATTR_ATIME_SET ) && + (unsigned long )iap->ia_atime.tv_nsec >=NSEC_PER_SEC ) +return false; +if ((iap->ia_valid &ATTR_MTIME_SET ) && + (unsigned long )iap->ia_mtime.tv_nsec >=NSEC_PER_SEC ) +return false; +return true; +} +static __be32 nfsd3_map_status (__be32 status) {switch (status) {
@@ -101,9 +120,14 @@ nfsd3_proc_setattr(struct svc_rqst *rqstp) SVCFH_fmt(&argp->fh));fh_copy (&resp->fh, &argp->fh); +if (!nfsd3_time_in_range (&argp->attrs)) { + resp->status = nfserr_inval; + goto out; + }if (argp->check_guard) guardtime = &argp->guardtime; resp->status =nfsd_setattr (rqstp, &resp->fh, &attrs, guardtime); +out: resp->status =nfsd3_map_status (resp->status);return rpc_success; }
@@ -265,6 +289,8 @@ nfsd3_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,trace_nfsd_vfs_create (rqstp, fhp,S_IFREG , argp->name, argp->len); +if (!nfsd3_time_in_range (iap)) +return nfserr_inval;if (isdotent (argp->name, argp->len))return nfserr_exist;if (!(iap->ia_valid &ATTR_MODE ))
@@ -400,8 +426,13 @@ nfsd3_proc_mkdir(struct svc_rqst *rqstp) argp->attrs.ia_valid &= ~ATTR_SIZE ;fh_copy (&resp->dirfh, &argp->fh);fh_init (&resp->fh,NFS3_FHSIZE ); +if (!nfsd3_time_in_range (&argp->attrs)) { + resp->status = nfserr_inval; + goto out; + } resp->status =nfsd_create (rqstp, &resp->dirfh, argp->name, argp->len, &attrs,S_IFDIR ,0 , &resp->fh); +out: resp->status =nfsd3_map_status (resp->status);return rpc_success; }
@@ -415,6 +446,10 @@ nfsd3_proc_symlink(struct svc_rqst *rqstp) .na_iattr = &argp->attrs, }; +if (!nfsd3_time_in_range (&argp->attrs)) { + resp->status = nfserr_inval; + goto out; + }if (argp->tlen ==0 ) { resp->status = nfserr_inval; goto out;
@@ -471,6 +506,11 @@ nfsd3_proc_mknod(struct svc_rqst *rqstp) goto out; } +if (!nfsd3_time_in_range (&argp->attrs)) { + resp->status = nfserr_inval; + goto out; + } + type = nfs3_ftypes[argp->ftype]; resp->status =nfsd_create (rqstp, &resp->dirfh, argp->name, argp->len, &attrs, type, rdev, &resp->fh);
Signature · 4 lines
2.43.0 Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.
Re: [PATCH v3 2/3] nfsd: reject out-of-range nseconds in NFSv3 SETATTR and create ops
Quoted 129 lines · robbieko
From: Robbie Ko <[email protected]>
A client can send an NFSv3 SETATTR, CREATE, MKDIR, SYMLINK or MKNOD carrying an atime or mtime whose nseconds field is out of range. The value is well-formed on the wire and decodes cleanly into a valid uint32, but it is not a valid timespec64:
tv_nsecmust be less thanNSEC_PER_SEC.Nothing in the setattr path clamps it.
notify_change()runs the time throughtimestamp_truncate(), which does not reducetv_nsecbelowNSEC_PER_SECwhen the filesystem supports nanosecond granularity (s_time_gran== 1), and the inode atime/mtime setters store it verbatim (only ctime is normalized, viainode_set_ctime_to_ts()). The un-normalized value then corrupts on-disk metadata: ext4'sext4_encode_extra_time()shiftstv_nsecleft byEXT4_EPOCH_BITS, which overflows the 32-bit extra field and clobbers the seconds-epoch bits, so the stored seconds (and thus the year) are wrong on read-back. XFS with bigtime mis-stores the timestamp for the same reason.Validate the client-supplied atime/mtime in the proc handlers and return
NFS3ERR_INVALbefore anything is changed. RFC 1813 listsNFS3ERR_INVALfor SETATTR and describes it as the error for a value the server 'can not store ... in its own representation'; the client maps it to EINVAL.Checking in the proc handlers, rather than in
nfsd_setattr(), keeps the rejection in front of object creation. The create operations create the object beforenfsd_create_setattr()runs, so a late failure would leave the new object behind and turn a non-idempotent request into a namespace change that reports failure. The check is therefore done up front, for the create operations before the object is created.
tv_nsecis a long, so the comparison casts it to unsigned long (the same width) rather than to u32, matchingtimespec64_valid(). A u32 cast would truncate on 64-bit; the unsigned long cast also rejects a value that became negative when an out-of-range u32 wire nseconds was assigned to a 32-bit long.Only client-supplied times are checked:
SET_TO_SERVER_TIMErequests carry no client value. The sattrguard3 ctime is deliberately left alone:
an out-of-range guard simply never matches the object's ctime and yieldsNFS3ERR_NOT_SYNCvia the existing guardtime comparison, which is the protocol-correct outcome rather than rejecting the request.Signed-off-by Robbie Ko <[email protected]>--- fs/nfsd/nfs3proc.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+)fs/nfsd/nfs3proc.c +35
-0@@ -29,6 +29,25 @@ static int nfs3_ftypes[] = { S_IFIFO, /* NF3FIFO */ }; +/* +* Reject a client-supplied atime or mtime whose nanoseconds field is out +* of range. Such a value is well-formed on the wire but is not a valid +* timespec64, and storing it verbatim can corrupt on-disk timestamps. +* tv_nsec is a long, so it is cast to unsigned long (the same width) to +* catch both an over-large value and one that became negative when an +* out-of-range u32 wire nseconds was assigned to a 32-bit long. +*/ +static bool nfsd3_time_in_range (const struct iattr *iap) +{ +if ((iap->ia_valid &ATTR_ATIME_SET ) && + (unsigned long )iap->ia_atime.tv_nsec >=NSEC_PER_SEC ) +return false; +if ((iap->ia_valid &ATTR_MTIME_SET ) && + (unsigned long )iap->ia_mtime.tv_nsec >=NSEC_PER_SEC ) +return false; +return true; +} +static __be32 nfsd3_map_status (__be32 status) {switch (status) {@@ -101,9 +120,14 @@ nfsd3_proc_setattr(struct svc_rqst *rqstp) SVCFH_fmt(&argp->fh));fh_copy (&resp->fh, &argp->fh); +if (!nfsd3_time_in_range (&argp->attrs)) { + resp->status = nfserr_inval; + goto out; + }if (argp->check_guard) guardtime = &argp->guardtime; resp->status =nfsd_setattr (rqstp, &resp->fh, &attrs, guardtime); +out: resp->status =nfsd3_map_status (resp->status);return rpc_success; }@@ -265,6 +289,8 @@ nfsd3_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,trace_nfsd_vfs_create (rqstp, fhp,S_IFREG , argp->name, argp->len); +if (!nfsd3_time_in_range (iap)) +return nfserr_inval;if (isdotent (argp->name, argp->len))return nfserr_exist;if (!(iap->ia_valid &ATTR_MODE ))@@ -400,8 +426,13 @@ nfsd3_proc_mkdir(struct svc_rqst *rqstp) argp->attrs.ia_valid &= ~ATTR_SIZE ;fh_copy (&resp->dirfh, &argp->fh);fh_init (&resp->fh,NFS3_FHSIZE ); +if (!nfsd3_time_in_range (&argp->attrs)) { + resp->status = nfserr_inval; + goto out; + } resp->status =nfsd_create (rqstp, &resp->dirfh, argp->name, argp->len, &attrs,S_IFDIR ,0 , &resp->fh); +out: resp->status =nfsd3_map_status (resp->status);return rpc_success; }@@ -415,6 +446,10 @@ nfsd3_proc_symlink(struct svc_rqst *rqstp) .na_iattr = &argp->attrs, }; +if (!nfsd3_time_in_range (&argp->attrs)) { + resp->status = nfserr_inval; + goto out; + }if (argp->tlen ==0 ) { resp->status = nfserr_inval; goto out;@@ -471,6 +506,11 @@ nfsd3_proc_mknod(struct svc_rqst *rqstp) goto out;
fs/nfsd/nfs3proc.c nfsd3_proc_mknod(struct svc_rqst *rqstp) · line 516
Quoted 10 lines
fs/nfsd/nfs3proc.c +5
-0} +if (!nfsd3_time_in_range (&argp->attrs)) { + resp->status = nfserr_inval; + goto out; + } + type = nfs3_ftypes[argp->ftype]; resp->status =nfsd_create (rqstp, &resp->dirfh, argp->name, argp->len, &attrs, type, rdev, &resp->fh);
PATCH v3 3/3 nfsd: use NSEC_PER_SEC in nfsd4_decode_nfstime4()
In reply to robbiekoFrom: Robbie Ko <[email protected]>
nfsd4_decode_nfstime4() open-codes the nanoseconds upper bound as the literal (u32)1000000000. Use the named constant NSEC_PER_SEC instead, matching the NFSv3 setattr check and improving readability.
The original code cast the literal to u32 to force an unsigned comparison, which matters on 32-bit where tv_nsec is a 32-bit signed
long: an out-of-range u32 wire nseconds (>= 0x80000000) assigned to it becomes negative and a signed compare against NSEC_PER_SEC (a signed long) would wrongly pass. Keep that protection by casting tv_nsec to unsigned long, the same width as tv_nsec, matching timespec64_valid(). No functional change.
--- fs/nfsd/nfs4xdr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/nfsd/nfs4xdr.c +1 -1
@@ -244,7 +244,7 @@ nfsd4_decode_nfstime4(struct nfsd4_compoundargs *argp, struct timespec64 *tv)return nfserr_bad_xdr; p =xdr_decode_hyper (p, &tv->tv_sec); tv->tv_nsec =be32_to_cpup (p++); -if (tv->tv_nsec >= (u32 )1000000000 ) +if ((unsigned long )tv->tv_nsec >=NSEC_PER_SEC )return nfserr_inval;return nfs_ok; }
Signature · 4 lines
2.43.0 Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.
Re: [PATCH v3 3/3] nfsd: use NSEC_PER_SEC in nfsd4_decode_nfstime4()
Quoted 24 lines · robbieko
From: Robbie Ko <[email protected]>
nfsd4_decode_nfstime4()open-codes the nanoseconds upper bound as the literal (u32)1000000000. Use the named constantNSEC_PER_SECinstead, matching the NFSv3 setattr check and improving readability.The original code cast the literal to u32 to force an unsigned comparison, which matters on 32-bit where
tv_nsecis a 32-bit signed
long: an out-of-range u32 wire nseconds (>=0x80000000) assigned to it becomes negative and a signed compare againstNSEC_PER_SEC(a signed long) would wrongly pass. Keep that protection by castingtv_nsecto unsigned long, the same width astv_nsec, matchingtimespec64_valid(). No functional change.Signed-off-by Robbie Ko <[email protected]>--- fs/nfsd/nfs4xdr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/nfsd/nfs4xdr.c +0
-0
fs/nfsd/nfs4xdr.c nfsd4_decode_nfstime4(struct nfsd4_compoundargs *argp, struct timespec64 *tv) · line 250
Quoted 8 lines
fs/nfsd/nfs4xdr.c +1
-1@@ -244,7 +244,7 @@ nfsd4_decode_nfstime4(struct nfsd4_compoundargs *argp, struct timespec64 *tv)return nfserr_bad_xdr; p =xdr_decode_hyper (p, &tv->tv_sec); tv->tv_nsec =be32_to_cpup (p++); -if (tv->tv_nsec >= (u32 )1000000000 ) +if ((unsigned long )tv->tv_nsec >=NSEC_PER_SEC )return nfserr_inval;return nfs_ok; }
Quoted 51 lines · robbieko
From: Robbie Ko <[email protected]>
The NFSv2 sattr decoder converts the wire useconds to nanoseconds in
svcxdr_decode_sattr():iap->ia_atime.tv_nsec = tmp2 * NSEC_PER_USEC;tmp2 is a u32 and
NSEC_PER_USECis 1000, so the product is computed in unsigned long. On ILP32 that is 32 bits, and an out-of-range useconds value such as 4294968 wraps totv_nsec== 704. The corruption therefore happens during decode, before any proc function can inspect the value, and a later range check ontv_nsecwould see an in-range result and accept it.Guard the raw useconds before the multiplication and reject values greater than 1000000. useconds == 1000000 is kept: it is the Sun convention for "set to the current server time", and the in-tree Linux NFSv2 client emits it in both the atime and the mtime field for a plain touch /
utimes(file, NULL)(seeencode_sattr()andxdr_encode_current_server_time()infs/nfs/nfs2xdr.c). Rejecting 1000000 would turn that common operation into a hard decode failure for both SETATTR and CREATE. 1000000 *NSEC_PER_USECis 10^9, which does not wrap on ILP32, so the Sun convention value passes through safely; only genuinely out-of-range values (> 1000000) are rejected. The atime and mtime guards are therefore symmetric.The decoder only applied the Sun convention in the mtime block, which clears ATTR_ATIME_SET|ATTR_MTIME_SET when mtime useconds == 1000000. If a client puts 1000000 in the atime field but not in the mtime field, the atime block stored an out-of-range
tv_nsec(10^9) and leftATTR_ATIME_SETset, so the bogus value reached the filesystem. Apply the convention in the atime block as well, clearingATTR_ATIME_SETso the server uses its current time and ignores the value. OnlyATTR_ATIME_SETis cleared there; the mtime block keeps its existing behavior, where 1000000 means "set both atime and mtime to now".Signed-off-by Robbie Ko <[email protected]>--- fs/nfsd/nfsxdr.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)fs/nfsd/nfsxdr.c +2
-0@@ -172,14 +172,45 @@ svcxdr_decode_sattr(struct svc_rqst *rqstp, struct xdr_stream *xdr, tmp1 =be32_to_cpup (p++); tmp2 =be32_to_cpup (p++);if (tmp1 != (u32)-1 && tmp2 != (u32)-1 ) { +/* +* Guard the raw useconds before the unit conversion below.
fs/nfsd/nfsxdr.c svcxdr_decode_sattr(struct svc_rqst *rqstp, struct xdr_stream *xdr, · line 186
Quoted 10 lines
fs/nfsd/nfsxdr.c +10
-0+* tmp2 * NSEC_PER_USEC is computed in unsigned long, which is +* 32 bits on ILP32, so an out-of-range value would wrap and +* silently produce a bogus in-range tv_nsec. useconds == +* 1000000 is the Sun "set to current server time" convention +* (see the mtime block below); allow it and reject anything +* larger. Note 1000000 * NSEC_PER_USEC is 10^9, which does not +* wrap on ILP32. +*/ +if (tmp2 >1000000 ) +return false;
The logic looks fine, but rather than having these verbose comments above each use of 1000000, it'd be better to declare a constant and document its use once above that.
Something like this (and consolidate the comments above that):
#define SUN_V2_SET_TO_NOW 1000000
Though I think the current fashion is to use enums for this so they show up in the debugger.
Quoted 30 lines
fs/nfsd/nfsxdr.c +19
-0iap->ia_valid |=ATTR_ATIME |ATTR_ATIME_SET ; iap->ia_atime.tv_sec = tmp1; iap->ia_atime.tv_nsec = tmp2 *NSEC_PER_USEC ; +/* +* The Linux NFSv2 client emits useconds == 1000000 in the +* atime field too (touch / utimes(file, NULL) sets ATTR_ATIME +* without ATTR_ATIME_SET). Apply the Sun convention here so +* the server uses its current time and ignores the bogus +* tv_nsec, instead of storing an out-of-range value when the +* mtime field does not also carry 1000000. Only ATTR_ATIME_SET +* is cleared; the mtime block keeps its own handling, where +* 1000000 means "set both atime and mtime to now". +*/ +if (tmp2 ==1000000 ) + iap->ia_valid &= ~ATTR_ATIME_SET ; } tmp1 =be32_to_cpup (p++); tmp2 =be32_to_cpup (p++);if (tmp1 != (u32)-1 && tmp2 != (u32)-1 ) { +/* +* useconds == 1000000 is a valid Sun convention here (see +* below); anything above that is out of range. Guard it before +* the unit conversion to avoid the ILP32 wrap described above. +*/ +if (tmp2 >1000000 ) +return false; iap->ia_valid |=ATTR_MTIME |ATTR_MTIME_SET ; iap->ia_mtime.tv_sec = tmp1; iap->ia_mtime.tv_nsec = tmp2 *NSEC_PER_USEC ;
Chuck Lever
maintainerQuoted 67 lines · Jeff Layton
Quoted 51 lines · robbieko
From: Robbie Ko <[email protected]>
The NFSv2 sattr decoder converts the wire useconds to nanoseconds in
svcxdr_decode_sattr():iap->ia_atime.tv_nsec = tmp2 * NSEC_PER_USEC;tmp2 is a u32 and
NSEC_PER_USECis 1000, so the product is computed in unsigned long. On ILP32 that is 32 bits, and an out-of-range useconds value such as 4294968 wraps totv_nsec== 704. The corruption therefore happens during decode, before any proc function can inspect the value, and a later range check ontv_nsecwould see an in-range result and accept it.Guard the raw useconds before the multiplication and reject values greater than 1000000. useconds == 1000000 is kept: it is the Sun convention for "set to the current server time", and the in-tree Linux NFSv2 client emits it in both the atime and the mtime field for a plain touch /
utimes(file, NULL)(seeencode_sattr()andxdr_encode_current_server_time()infs/nfs/nfs2xdr.c). Rejecting 1000000 would turn that common operation into a hard decode failure for both SETATTR and CREATE. 1000000 *NSEC_PER_USECis 10^9, which does not wrap on ILP32, so the Sun convention value passes through safely; only genuinely out-of-range values (> 1000000) are rejected. The atime and mtime guards are therefore symmetric.The decoder only applied the Sun convention in the mtime block, which clears ATTR_ATIME_SET|ATTR_MTIME_SET when mtime useconds == 1000000. If a client puts 1000000 in the atime field but not in the mtime field, the atime block stored an out-of-range
tv_nsec(10^9) and leftATTR_ATIME_SETset, so the bogus value reached the filesystem. Apply the convention in the atime block as well, clearingATTR_ATIME_SETso the server uses its current time and ignores the value. OnlyATTR_ATIME_SETis cleared there; the mtime block keeps its existing behavior, where 1000000 means "set both atime and mtime to now".Signed-off-by Robbie Ko <[email protected]>--- fs/nfsd/nfsxdr.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)fs/nfsd/nfsxdr.c +2
-0@@ -172,14 +172,45 @@ svcxdr_decode_sattr(struct svc_rqst *rqstp, struct xdr_stream *xdr, tmp1 =be32_to_cpup (p++); tmp2 =be32_to_cpup (p++);if (tmp1 != (u32)-1 && tmp2 != (u32)-1 ) { +/* +* Guard the raw useconds before the unit conversion below. fs/nfsd/nfsxdr.c svcxdr_decode_sattr(struct svc_rqst *rqstp, struct xdr_stream *xdr, · line 186
Quoted 10 lines
fs/nfsd/nfsxdr.c +10
-0+* tmp2 * NSEC_PER_USEC is computed in unsigned long, which is +* 32 bits on ILP32, so an out-of-range value would wrap and +* silently produce a bogus in-range tv_nsec. useconds == +* 1000000 is the Sun "set to current server time" convention +* (see the mtime block below); allow it and reject anything +* larger. Note 1000000 * NSEC_PER_USEC is 10^9, which does not +* wrap on ILP32. +*/ +if (tmp2 >1000000 ) +return false;The logic looks fine, but rather than having these verbose comments above each use of 1000000, it'd be better to declare a constant and document its use once above that.
Quoted 6 lines
Something like this (and consolidate the comments above that):
#define
SUN_V2_SET_TO_NOW1000000Though I think the current fashion is to use enums for this so they show up in the debugger.
The applied version of this patch defines such a symbolic constant. Robbie, no need to resend.
Signature · 1 line
Chuck Lever
Chuck Lever
maintainerQuoted 5 lines · robbieko
The NFSv2 sattr decoder converts the wire useconds to nanoseconds in
svcxdr_decode_sattr():iap->ia_atime.tv_nsec = tmp2 * NSEC_PER_USEC;
Quoted 8 lines
tmp2 is a u32 and
NSEC_PER_USECis 1000, so the product is computed in unsigned long. On ILP32 that is 32 bits, and an out-of-range useconds value such as 4294968 wraps totv_nsec== 704. The corruption therefore happens during decode, before any proc function can inspect the value, and a later range check ontv_nsecwould see an in-range result and accept it.[...]
Applied to nfsd-testing, thanks!
[1/3] nfsd: reject out-of-range useconds in NFSv2 SETATTR/CREATE
commit: 5a6e45730d33afefe6ac1208173365840314dfd9
[2/3] nfsd: reject out-of-range nseconds in NFSv3 SETATTR and create ops
commit: ccf2a0a8a51b8fa23bd4e4413e01b57a81451c7f
[3/3] nfsd: use NSEC_PER_SEC in nfsd4_decode_nfstime4()
commit: 324975079e798c30a15d23396c51216937389baf--
Chuck Lever