Skip to contentSkip to conversation

NFSv4: prevent integer overflow while calling nfs4_set_lease_period()

PATCH Superseded Sergey Shtylyov 2 participants 5 messages Aug 23, 2024 – Jun 28, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 *unsigned long* on 32-bit arches. 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 (using mul_u32_u32(), as it produces a better code on 32-bit x86 arch), also checking for an overflow there and returning -ERANGE if it does happen (we're also making that function *int* instead of *void* and adding the result checks to its callers)...

Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.

---
This patch is against the master branch of Trond Myklebust's linux-nfs.git repo.

 fs/nfs/nfs4_fs.h    |    3 +--
 fs/nfs/nfs4proc.c   |    3 ++-
 fs/nfs/nfs4renewd.c |   13 ++++++++++---
 fs/nfs/nfs4state.c  |    4 +++-
 4 files changed, 16 insertions(+), 7 deletions(-)
fs/nfs/nfs4_fs.h +1 -2
Index: linux-nfs/fs/nfs/nfs4_fs.h
===================================================================
@@ -464,8 +464,7 @@ struct nfs_client *nfs4_alloc_client(con
 extern 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 int nfs4_set_lease_period(struct nfs_client *clp, u32 period);
 
 /* nfs4state.c */
 extern const nfs4_stateid current_stateid;
fs/nfs/nfs4proc.c +2 -1
Index: linux-nfs/fs/nfs/nfs4proc.c
===================================================================
@@ -5403,7 +5403,8 @@ 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);
+			err = nfs4_set_lease_period(server->nfs_client,
+						    fsinfo->lease_time);
 			break;
 		}
 		err = nfs4_handle_exception(server, err, &exception);
fs/nfs/nfs4renewd.c +10 -3
Index: linux-nfs/fs/nfs/nfs4renewd.c
===================================================================
@@ -137,15 +137,22 @@ 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)
+int nfs4_set_lease_period(struct nfs_client *clp, u32 period)
 {
+	u64 result = mul_u32_u32(period, HZ);
+	unsigned long lease = result;
+
+	/* First see if period * HZ actually fits into unsigned long... */
+	if (result > ULONG_MAX)
+		return -ERANGE;
+
 	spin_lock(&clp->cl_lock);
 	clp->cl_lease_time = lease;
 	spin_unlock(&clp->cl_lock);
 
 	/* Cap maximum reconnect timeout at 1/2 lease period */
 	rpc_set_connect_timeout(clp->cl_rpcclient, lease, lease >> 1);
+	return 0;
 }
fs/nfs/nfs4state.c +3 -1
Index: linux-nfs/fs/nfs/nfs4state.c
===================================================================
@@ -103,7 +103,9 @@ 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);
+		status = nfs4_set_lease_period(clp, fsinfo.lease_time);
+		if (status)
+			return status;
 		nfs4_schedule_state_renewal(clp);
 	}
 
#1 lore raw

Hi Sergey,

Quoted 61 lines · Sergey Shtylyov

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 *unsigned long* on 32-bit arches. 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 (using mul_u32_u32(), as it produces a better code on 32-bit x86 arch), also checking for an overflow there and returning -ERANGE if it does happen (we're also making that function *int* instead of *void* and adding the result checks to its callers)...

Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.

---
This patch is against the master branch of Trond Myklebust's linux-nfs.git repo.

 fs/nfs/nfs4_fs.h    |    3 +--
 fs/nfs/nfs4proc.c   |    3 ++-
 fs/nfs/nfs4renewd.c |   13 ++++++++++---
 fs/nfs/nfs4state.c  |    4 +++-
 4 files changed, 16 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(con
 extern 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 int 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 +2 -1
@@ -5403,7 +5403,8 @@ 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);
+			err = 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 -0
@@ -137,15 +137,22 @@ nfs4_kill_renewd(struct nfs_client *clp)
  * nfs4_set_lease_period - Sets the lease period on a nfs_client

fs/nfs/nfs4renewd.c nfs4_kill_renewd(struct nfs_client *clp) · line 144

Quoted 10 lines
fs/nfs/nfs4renewd.c +3 -3
  *
  * @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)
+int nfs4_set_lease_period(struct nfs_client *clp, u32 period)
 {
+	u64 result = mul_u32_u32(period, HZ);

Thanks for the patch! Sorry it took a couple of weeks for me to look at. I do like this change for doing the calculation in a single place, rather than relying on callers to do it first.

fs/nfs/nfs4renewd.c nfs4_kill_renewd(struct nfs_client *clp) · line 149

Quoted 5 lines
fs/nfs/nfs4renewd.c +5 -0
+	unsigned long lease = result;
+
+	/* First see if period * HZ actually fits into unsigned long... */
+	if (result > ULONG_MAX)
+		return -ERANGE;

However, I'm not sold that this should be an error condition. I wonder if it would be better to change the clp->cl_lease_time field to a u64 so it has the same size on 32bit and 64bit architectures?

Thanks,
Anna

Quoted 24 lines
fs/nfs/nfs4renewd.c +2 -0
+
 	spin_lock(&clp->cl_lock);
 	clp->cl_lease_time = lease;
 	spin_unlock(&clp->cl_lock);
 
 	/* Cap maximum reconnect timeout at 1/2 lease period */
 	rpc_set_connect_timeout(clp->cl_rpcclient, lease, lease >> 1);
+	return 0;
 }

Index: linux-nfs/fs/nfs/nfs4state.c
===================================================================

fs/nfs/nfs4state.c +3 -1
@@ -103,7 +103,9 @@ 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);
+		status = nfs4_set_lease_period(clp, fsinfo.lease_time);
+		if (status)
+			return status;
 		nfs4_schedule_state_renewal(clp);
 	}
 
#2 lore raw

On 9/16/24 22:44, Anna Schumaker wrote:
[...]

Quoted 17 lines
Quoted 17 lines

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 *unsigned long* on 32-bit arches. 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 (using mul_u32_u32(), as it produces a better code on 32-bit x86 arch), also checking for an overflow there and returning -ERANGE if it does happen (we're also making that function *int* instead of *void* and adding the result checks to its callers)...

Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.

[...]

Quoted 10 lines
Quoted 10 lines

Index: linux-nfs/fs/nfs/nfs4renewd.c
===================================================================

fs/nfs/nfs4renewd.c +1 -1
@@ -137,15 +137,22 @@ 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)
Quoted 8 lines

fs/nfs/nfs4renewd.c nfs4_kill_renewd(struct nfs_client *clp) · line 144

Quoted 6 lines
fs/nfs/nfs4renewd.c +2 -2
  */
-void nfs4_set_lease_period(struct nfs_client *clp,
-		unsigned long lease)
+int nfs4_set_lease_period(struct nfs_client *clp, u32 period)
 {
+	u64 result = mul_u32_u32(period, HZ);

Thanks for the patch! Sorry it took a couple of weeks for me to look at.

NP, better late than never. :-)

Quoted 1 line

I do like this change for doing the calculation in a single place, rather

than relying on callers to do it first.

Quoted 8 lines

fs/nfs/nfs4renewd.c nfs4_kill_renewd(struct nfs_client *clp) · line 149

Quoted 6 lines
fs/nfs/nfs4renewd.c +5 -0
+	unsigned long lease = result;
+
+	/* First see if period * HZ actually fits into unsigned long... */
+	if (result > ULONG_MAX)
+		return -ERANGE;

However, I'm not sold that this should be an error condition. I wonder if it would be better to change the clp->cl_lease_time field to a u64 so it has the same size on 32bit and 64bit architectures?

That would be much more invasive change. And pretty fruitless, I think, because nfs_client::cl_lease_time is only involved in the jiffies math,
and since jiffies is *unsigned long* (i.e. 32-bit on a 32-bit arch), the
results of our u64 math would get truncated to 32 bits anyway in the end...
Looking at time_{after|before}(), I even suspected they could complain
at compile time -- however I wasn't able to trigger that so far...

Quoted 2 lines

Thanks,
Anna

[...]

MBR, Sergey

#3 lore raw

On 9/18/24 11:52 PM, Sergey Shtylyov wrote:
[...]

Quoted 24 lines
Quoted 17 lines
Quoted 17 lines

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 *unsigned long* on 32-bit arches. 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 (using mul_u32_u32(), as it produces a better code on 32-bit x86 arch), also checking for an overflow there and returning -ERANGE if it does happen (we're also making that function *int* instead of *void* and adding the result checks to its callers)...

Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.

[...]

Quoted 5 lines
Quoted 5 lines

Index: linux-nfs/fs/nfs/nfs4renewd.c
===================================================================

fs/nfs/nfs4renewd.c +0 -0
@@ -137,15 +137,22 @@ nfs4_kill_renewd(struct nfs_client *clp)

[...]

Quoted 8 lines
Quoted 7 lines

fs/nfs/nfs4renewd.c nfs4_kill_renewd(struct nfs_client *clp) · line 149

Quoted 5 lines
fs/nfs/nfs4renewd.c +5 -0
+	unsigned long lease = result;
+
+	/* First see if period * HZ actually fits into unsigned long... */
+	if (result > ULONG_MAX)
+		return -ERANGE;

However, I'm not sold that this should be an error condition. I wonder if it would be better to change the clp->cl_lease_time field to a u64 so it has the same size on 32bit and 64bit architectures?

Quoted 5 lines

That would be much more invasive change. And pretty fruitless, I think, because nfs_client::cl_lease_time is only involved in the jiffies math,
and since jiffies is *unsigned long* (i.e. 32-bit on a 32-bit arch), the
results of our u64 math would get truncated to 32 bits anyway in the end...
Looking at time_{after|before}(), I even suspected they could complain
at compile time -- however I wasn't able to trigger that so far...

Alternatively, we could set the lease variable on overflow to MAX_JIFFY_OFFSET
which seems to be a max timeout in jiffies...

[...]

MBR, Sergey

#4 lore raw
In reply to Sergey Shtylyov
Quoted 6 lines · Sergey Shtylyov

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 *unsigned long* on 32-bit arches. Actually, there's no need to multiply by HZ at all the call sites

As the multiplication by HZ is performed on 32-bit field, the overflow
will occur regardless of 32/64-bitness...

Quoted 11 lines

of nfs4_set_lease_period() -- it makes more sense to do that once, inside that function (using mul_u32_u32(), as it produces a better code on 32-bit x86 arch), also checking for an overflow there and returning -ERANGE if it does happen (we're also making that function *int* instead of *void* and adding the result checks to its callers)...

Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.

[...]

Quoted 12 lines

Index: linux-nfs/fs/nfs/nfs4renewd.c
===================================================================

fs/nfs/nfs4renewd.c +1 -2
@@ -137,15 +137,22 @@ 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,

fs/nfs/nfs4renewd.c nfs4_kill_renewd(struct nfs_client *clp) · line 150

Quoted 10 lines
fs/nfs/nfs4renewd.c +8 -1
-		unsigned long lease)
+int nfs4_set_lease_period(struct nfs_client *clp, u32 period)
 {
+	u64 result = mul_u32_u32(period, HZ);
+	unsigned long lease = result;
+
+	/* First see if period * HZ actually fits into unsigned long... */
+	if (result > ULONG_MAX)
+		return -ERANGE;
+

Now that I have discovered check_mul_overflow(), I think I'll have to
rewrite this code using it instead...

[...]

MBR, Sergey