NFSv4: prevent integer overflow while calling nfs4_set_lease_period()
Subsystems (2)
PATCH v3 1/1 NFSv4: prevent integer overflow while calling nfs4_set_lease_period()
The nfs_client::cl_lease_time field (as well as the jiffies variable it's used with) is declared as *unsigned long*, which is 32-bit type on 32-bit arches and 64-bit type on 64-bit arches. When nfs4_set_lease_period() that sets nfs_client::cl_lease_time is called, 32-bit nfs_fsinfo::lease_time field is multiplied by HZ -- that might overflow before being implicitly cast to *unsigned long*. Actually, there's no need to multiply by HZ at all the call sites of nfs4_set_lease_period() -- it makes more sense to do that once, inside that function, calling check_mul_overflow() and falling back to 1 hour on an actual overflow...
Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.
---
The patch is against the master branch of Trond Myklebust's linux-nfs.git repo.
Changes in version #3:
- changed the lease period overflow fallback to 1 hour, updated the patch description accordingly.
Changes in version #2:
- made use of check_mul_overflow() instead of mul_u32_u32();
- capped the multiplication result at ULONG_MAX instead of returning -ERANGE, keeping nfs4_set_lease_period() *void*;
- rewrote the patch description accordingly.
fs/nfs/nfs4_fs.h | 3 +-- fs/nfs/nfs4proc.c | 2 +- fs/nfs/nfs4renewd.c | 10 +++++++--- fs/nfs/nfs4state.c | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-)
fs/nfs/nfs4_fs.h +1 -2
@@ -464,8 +464,7 @@ struct nfs_client *nfs4_alloc_client(conextern void nfs4_schedule_state_renewal (struct nfs_client *);extern void nfs4_kill_renewd (struct nfs_client *);extern void nfs4_renew_state (struct work_struct *); -extern void nfs4_set_lease_period (struct nfs_client *clp,unsigned long lease); - +extern void nfs4_set_lease_period (struct nfs_client *clp,u32 period);/* nfs4state.c */ extern const nfs4_stateid current_stateid;
fs/nfs/nfs4proc.c +1 -1
@@ -5478,7 +5478,7 @@ static int nfs4_do_fsinfo(struct nfs_ser err =_nfs4_do_fsinfo (server, fhandle, fsinfo);trace_nfs4_fsinfo (server, fhandle, fsinfo->fattr, err);if (err ==0 ) { -nfs4_set_lease_period (server->nfs_client, fsinfo->lease_time *HZ ); +nfs4_set_lease_period (server->nfs_client, fsinfo->lease_time);break ; } err =nfs4_handle_exception (server, err, &exception);
fs/nfs/nfs4renewd.c +7 -3
@@ -137,11 +137,15 @@ nfs4_kill_renewd(struct nfs_client *clp)* nfs4_set_lease_period - Sets the lease period on a nfs_client * * @clp: pointer to nfs_client -* @lease: new value for lease period +* @period: new value for lease period (in seconds) */ -void nfs4_set_lease_period (struct nfs_client *clp, -unsigned long lease) +void nfs4_set_lease_period (struct nfs_client *clp,u32 period) { +unsigned long lease; + +if (check_mul_overflow (period,HZ , &lease)) + lease =60UL *60UL *HZ ;/* one hour */ +spin_lock (&clp->cl_lock); clp->cl_lease_time = lease;spin_unlock (&clp->cl_lock);
fs/nfs/nfs4state.c +1 -1
@@ -103,7 +103,7 @@ static int nfs4_setup_state_renewal(stru status =nfs4_proc_get_lease_time (clp, &fsinfo);if (status ==0 ) { -nfs4_set_lease_period (clp, fsinfo.lease_time *HZ ); +nfs4_set_lease_period (clp, fsinfo.lease_time);nfs4_schedule_state_renewal (clp); }
Quoted 71 lines · On Thu, 13 Nov 2025 23:37:03 +0300 Sergey Shtylyov <[email protected]> wrote:
The
nfs_client::cl_lease_timefield (as well as the jiffies variable it's used with) is declared as *unsigned long*, which is 32-bit type on 32-bit arches and 64-bit type on 64-bit arches. Whennfs4_set_lease_period()that setsnfs_client::cl_lease_timeis called, 32-bitnfs_fsinfo::lease_timefield is multiplied by HZ -- that might overflow before being implicitly cast to *unsigned long*. Actually, there's no need to multiply by HZ at all the call sites ofnfs4_set_lease_period()-- it makes more sense to do that once, inside that function, callingcheck_mul_overflow()and falling back to 1 hour on an actual overflow...Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.
Signed-off-by Sergey Shtylyov <[email protected]>---
The patch is against the master branch of Trond Myklebust's linux-nfs.git repo.Changes in version #3:
- changed the lease period overflow fallback to 1 hour, updated the patch description accordingly.Changes in version #2:
- made use ofcheck_mul_overflow()instead ofmul_u32_u32();
- capped the multiplication result atULONG_MAXinstead of returning-ERANGE, keepingnfs4_set_lease_period()*void*;
- rewrote the patch description accordingly.fs/nfs/nfs4_fs.h | 3 +-- fs/nfs/nfs4proc.c | 2 +- fs/nfs/nfs4renewd.c | 10 +++++++--- fs/nfs/nfs4state.c | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-)Index:
linux-nfs/fs/nfs/nfs4_fs.h
===================================================================fs/nfs/nfs4_fs.h +1
-2@@ -464,8 +464,7 @@ struct nfs_client *nfs4_alloc_client(conextern void nfs4_schedule_state_renewal (struct nfs_client *);extern void nfs4_kill_renewd (struct nfs_client *);extern void nfs4_renew_state (struct work_struct *); -extern void nfs4_set_lease_period (struct nfs_client *clp,unsigned long lease); - +extern void nfs4_set_lease_period (struct nfs_client *clp,u32 period);/* nfs4state.c */ extern const nfs4_stateid current_stateid;Index:
linux-nfs/fs/nfs/nfs4proc.c
===================================================================fs/nfs/nfs4proc.c +1
-1@@ -5478,7 +5478,7 @@ static int nfs4_do_fsinfo(struct nfs_ser err =_nfs4_do_fsinfo (server, fhandle, fsinfo);trace_nfs4_fsinfo (server, fhandle, fsinfo->fattr, err);if (err ==0 ) { -nfs4_set_lease_period (server->nfs_client, fsinfo->lease_time *HZ ); +nfs4_set_lease_period (server->nfs_client, fsinfo->lease_time);break ; } err =nfs4_handle_exception (server, err, &exception);Index:
linux-nfs/fs/nfs/nfs4renewd.c
===================================================================fs/nfs/nfs4renewd.c +0
-1@@ -137,11 +137,15 @@ nfs4_kill_renewd(struct nfs_client *clp)* nfs4_set_lease_period - Sets the lease period on a nfs_client * * @clp: pointer to nfs_client -* @lease: new value for lease period
fs/nfs/nfs4renewd.c nfs4_kill_renewd(struct nfs_client *clp) · line 147
Quoted 10 lines
fs/nfs/nfs4renewd.c +6
-2+* @period: new value for lease period (in seconds) */ -void nfs4_set_lease_period (struct nfs_client *clp, -unsigned long lease) +void nfs4_set_lease_period (struct nfs_client *clp,u32 period) { +unsigned long lease; + +if (check_mul_overflow (period,HZ , &lease)) + lease =60UL *60UL *HZ ;/* one hour */
That isn't good enough, just a few lines higher there is:
timeout = (2 * clp->cl_lease_time) / 3 + (long)clp->cl_last_renewal - (long)jiffies;
So the value has to be multipliable by 2 without overflowing. I also suspect the modulo arithmetic also only works if the values are 'much smaller than long'.
With HZ = 1000 and a requested period of 1000 hours the multiply (just) fits in 32 bits - but none of the code is going to work at all.
It would be simpler and safer to just put a sanity upper limit on period. I've no idea what normal/sane values are (lower as well as upper). But perhaps you want:
/* For sanity clamp between 10 mins and 100 hours */ lease = clamp(period, 10 * 60, 100 * 60 * 60) * HZ;
fs/nfs/nfs4renewd.c nfs4_kill_renewd(struct nfs_client *clp) · line 151
Quoted 4 lines
fs/nfs/nfs4renewd.c +1
-0+spin_lock (&clp->cl_lock); clp->cl_lease_time = lease;spin_unlock (&clp->cl_lock);
Do I see a lock that doesn't?
David
Quoted 14 lines
Index:
linux-nfs/fs/nfs/nfs4state.c
===================================================================fs/nfs/nfs4state.c +1
-1@@ -103,7 +103,7 @@ static int nfs4_setup_state_renewal(stru status =nfs4_proc_get_lease_time (clp, &fsinfo);if (status ==0 ) { -nfs4_set_lease_period (clp, fsinfo.lease_time *HZ ); +nfs4_set_lease_period (clp, fsinfo.lease_time);nfs4_schedule_state_renewal (clp); }
On 11/14/25 1:41 AM, David Laight wrote:
[...]
Quoted 10 lines
Quoted 10 lines
The
nfs_client::cl_lease_timefield (as well as the jiffies variable it's used with) is declared as *unsigned long*, which is 32-bit type on 32-bit arches and 64-bit type on 64-bit arches. Whennfs4_set_lease_period()that setsnfs_client::cl_lease_timeis called, 32-bitnfs_fsinfo::lease_timefield is multiplied by HZ -- that might overflow before being implicitly cast to *unsigned long*. Actually, there's no need to multiply by HZ at all the call sites ofnfs4_set_lease_period()-- it makes more sense to do that once, inside that function, callingcheck_mul_overflow()and falling back to 1 hour on an actual overflow...
Quoted 5 lines
Quoted 5 lines
Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.
Signed-off-by Sergey Shtylyov <[email protected]>
[...]>> Index: linux-nfs/fs/nfs/nfs4renewd.c
Quoted 20 lines
Quoted 8 lines
===================================================================
fs/nfs/nfs4renewd.c +0
-1@@ -137,11 +137,15 @@ nfs4_kill_renewd(struct nfs_client *clp)* nfs4_set_lease_period - Sets the lease period on a nfs_client * * @clp: pointer to nfs_client -* @lease: new value for lease period fs/nfs/nfs4renewd.c nfs4_kill_renewd(struct nfs_client *clp) · line 147
Quoted 10 lines
fs/nfs/nfs4renewd.c +6
-2+* @period: new value for lease period (in seconds) */ -void nfs4_set_lease_period (struct nfs_client *clp, -unsigned long lease) +void nfs4_set_lease_period (struct nfs_client *clp,u32 period) { +unsigned long lease; + +if (check_mul_overflow (period,HZ , &lease)) + lease =60UL *60UL *HZ ;/* one hour */ That isn't good enough, just a few lines higher there is:
Quoted 2 lines
timeout = (2 * clp->cl_lease_time) / 3 + (long)clp->cl_last_renewal - (long)jiffies;
Indeed, I should have probably capped period at 3600 secs as well...
Quoted 3 lines
So the value has to be multipliable by 2 without overflowing. I also suspect the modulo arithmetic also only works if the values are 'much smaller than long'.
You mean the jiffies-relative math? I think it should work with any
values, with either 32- or 64-bit *unsigned long*...
Quoted 4 lines
With HZ = 1000 and a requested period of 1000 hours the multiply (just) fits in 32 bits - but none of the code is going to work at all.
It would be simpler and safer to just put a sanity upper limit on period.
Yes.
Quoted 1 line
I've no idea what normal/sane values are (lower as well as upper).
The RFCs don't have any, it seems...
Quoted 3 lines
But perhaps you want:
/* For sanity clamp between 10 mins and 100 hours */ lease = clamp(period, 10 * 60, 100 * 60 * 60) * HZ;
Trond was talking about 1-hour period... And I don't think we need the
lower bound (except maybe 1 second?)...
Quoted 6 lines
fs/nfs/nfs4renewd.c nfs4_kill_renewd(struct nfs_client *clp) · line 151
Quoted 4 lines
fs/nfs/nfs4renewd.c +1
-0+spin_lock (&clp->cl_lock); clp->cl_lease_time = lease;spin_unlock (&clp->cl_lock);Do I see a lock that doesn't?
Doesn't do anything useful, you mean? :-)
[...]
MBR, Sergey
Quoted 42 lines · On Tue, 18 Nov 2025 22:51:09 +0300 Sergey Shtylyov <[email protected]> wrote:
On 11/14/25 1:41 AM, David Laight wrote:
[...]Quoted 10 lines
Quoted 10 lines
The
nfs_client::cl_lease_timefield (as well as the jiffies variable it's used with) is declared as *unsigned long*, which is 32-bit type on 32-bit arches and 64-bit type on 64-bit arches. Whennfs4_set_lease_period()that setsnfs_client::cl_lease_timeis called, 32-bitnfs_fsinfo::lease_timefield is multiplied by HZ -- that might overflow before being implicitly cast to *unsigned long*. Actually, there's no need to multiply by HZ at all the call sites ofnfs4_set_lease_period()-- it makes more sense to do that once, inside that function, callingcheck_mul_overflow()and falling back to 1 hour on an actual overflow...Quoted 5 lines
Quoted 5 lines
Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.
Signed-off-by Sergey Shtylyov <[email protected]>[...]>> Index:
linux-nfs/fs/nfs/nfs4renewd.cQuoted 20 lines
Quoted 8 lines
===================================================================
fs/nfs/nfs4renewd.c +0
-1@@ -137,11 +137,15 @@ nfs4_kill_renewd(struct nfs_client *clp)* nfs4_set_lease_period - Sets the lease period on a nfs_client * * @clp: pointer to nfs_client -* @lease: new value for lease period fs/nfs/nfs4renewd.c nfs4_kill_renewd(struct nfs_client *clp) · line 147
Quoted 10 lines
fs/nfs/nfs4renewd.c +6
-2+* @period: new value for lease period (in seconds) */ -void nfs4_set_lease_period (struct nfs_client *clp, -unsigned long lease) +void nfs4_set_lease_period (struct nfs_client *clp,u32 period) { +unsigned long lease; + +if (check_mul_overflow (period,HZ , &lease)) + lease =60UL *60UL *HZ ;/* one hour */ That isn't good enough, just a few lines higher there is:
Quoted 2 lines
timeout = (2 * clp->cl_lease_time) / 3 + (long)clp->cl_last_renewal - (long)jiffies;
Quoted 8 lines
Indeed, I should have probably capped period at 3600 secs as well...
Quoted 3 lines
So the value has to be multipliable by 2 without overflowing. I also suspect the modulo arithmetic also only works if the values are 'much smaller than long'.
You mean the jiffies-relative math? I think it should work with any
values, with either 32- or 64-bit *unsigned long*...
There might be tests of the form (signed long)(jiffies - value) > 0. They only work if the interval is less than half (the time) of jiffies. Such values are insane - but you are applying a cap that isn't strong enough.
Quoted 10 lines
Quoted 5 lines
With HZ = 1000 and a requested period of 1000 hours the multiply (just) fits in 32 bits - but none of the code is going to work at all.
It would be simpler and safer to just put a sanity upper limit on period.
Yes.
Quoted 1 line
I've no idea what normal/sane values are (lower as well as upper).
Quoted 8 lines
The RFCs don't have any, it seems...
Quoted 3 lines
But perhaps you want:
/* For sanity clamp between 10 mins and 100 hours */ lease = clamp(period, 10 * 60, 100 * 60 * 60) * HZ;Trond was talking about 1-hour period... And I don't think we need the
lower bound (except maybe 1 second?)...
If 1 hour might be a reasonable value, then clamp to something much bigger that won't break the code.
In the past I've used at least twice the limit from the 'standard'. I've also had methods of setting values outside the limits (useful for testing timers that are supposed to be at least 10 minutes.
Quoted 9 lines
Quoted 7 lines
fs/nfs/nfs4renewd.c nfs4_kill_renewd(struct nfs_client *clp) · line 151
Quoted 5 lines
fs/nfs/nfs4renewd.c +1
-0+spin_lock (&clp->cl_lock); clp->cl_lease_time = lease;spin_unlock (&clp->cl_lock);Do I see a lock that doesn't?
Doesn't do anything useful, you mean? :-)
You can think of a 'lock' as something that locks two or more operations together - often just a compare and update.
That one is (at most) a WRITE_ONCE().
David
Quoted 4 lines
[...]
MBR, Sergey
On 11/19/25 12:17 AM, David Laight wrote:
[...]
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 15 lines
The
nfs_client::cl_lease_timefield (as well as the jiffies variable it's used with) is declared as *unsigned long*, which is 32-bit type on 32-bit arches and 64-bit type on 64-bit arches. Whennfs4_set_lease_period()that setsnfs_client::cl_lease_timeis called, 32-bitnfs_fsinfo::lease_timefield is multiplied by HZ -- that might overflow before being implicitly cast to *unsigned long*. Actually, there's no need to multiply by HZ at all the call sites ofnfs4_set_lease_period()-- it makes more sense to do that once, inside that function, callingcheck_mul_overflow()and falling back to 1 hour on an actual overflow...Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.
Signed-off-by Sergey Shtylyov <[email protected]>
[...]
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 9 lines
Index:
linux-nfs/fs/nfs/nfs4renewd.c
===================================================================fs/nfs/nfs4renewd.c +0
-1@@ -137,11 +137,15 @@ nfs4_kill_renewd(struct nfs_client *clp)* nfs4_set_lease_period - Sets the lease period on a nfs_client * * @clp: pointer to nfs_client -* @lease: new value for lease period fs/nfs/nfs4renewd.c nfs4_kill_renewd(struct nfs_client *clp) · line 147
Quoted 10 lines
fs/nfs/nfs4renewd.c +6
-2+* @period: new value for lease period (in seconds) */ -void nfs4_set_lease_period (struct nfs_client *clp, -unsigned long lease) +void nfs4_set_lease_period (struct nfs_client *clp,u32 period) { +unsigned long lease; + +if (check_mul_overflow (period,HZ , &lease)) + lease =60UL *60UL *HZ ;/* one hour */ That isn't good enough, just a few lines higher there is:
timeout = (2 * clp->cl_lease_time) / 3 + (long)clp->cl_last_renewal - (long)jiffies;
Quoted 1 line
Quoted 1 line
Indeed, I should have probably capped period at 3600 secs as well...
That's one hour.
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines
So the value has to be multipliable by 2 without overflowing. I also suspect the modulo arithmetic also only works if the values are 'much smaller than long'.
You mean the jiffies-relative math? I think it should work with any
values, with either 32- or 64-bit *unsigned long*...There might be tests of the form (signed long)(jiffies - value) > 0. They only work if the interval is less than half (the time) of jiffies. Such values are insane - but you are applying a cap that isn't strong enough.
* 3600 seconds, you mean?
Quoted 10 lines
Quoted 10 lines
Quoted 4 lines
With HZ = 1000 and a requested period of 1000 hours the multiply (just) fits in 32 bits - but none of the code is going to work at all.
It would be simpler and safer to just put a sanity upper limit on period.
Yes.
Quoted 1 line
I've no idea what normal/sane values are (lower as well as upper).
The RFCs don't have any, it seems...
Nether max nor min. It's on;y said that lease_time is a 32-bit unsigned
value...
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
But perhaps you want:
/* For sanity clamp between 10 mins and 100 hours */ lease = clamp(period, 10 * 60, 100 * 60 * 60) * HZ;Trond was talking about 1-hour period... And I don't think we need the
lower bound (except maybe 1 second?)...If 1 hour might be a reasonable value, then clamp to something much bigger that won't break the code.
Trond said that even that seems too much for the file lock lease period...
[...]
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
fs/nfs/nfs4renewd.c +1
-0+spin_lock (&clp->cl_lock); clp->cl_lease_time = lease;spin_unlock (&clp->cl_lock);
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Do I see a lock that doesn't?
Doesn't do anything useful, you mean? :-)
You can think of a 'lock' as something that locks two or more operations together - often just a compare and update.
That one is (at most) a
WRITE_ONCE().
Yes, probably... :-)
[..]
MBR, Sergey