Skip to contentSkip to conversation

fs: nfsd: Fix three possible null-pointer dereferences

PATCH In review Jia-Ju Bai 2 participants 3 messages Jul 24, 2019 – Jul 30, 2019

Subsystems (2)
lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

PATCH 1/1 fs: nfsd: Fix three possible null-pointer dereferences

In nfs4_xdr_dec_cb_recall(), nfs4_xdr_dec_cb_layout() and nfs4_xdr_dec_cb_notify_lock(), there is an if statement to check whether cb is NULL.

When cb is NULL, the three functions all call:

    decode_cb_op_status(..., &cb->cb_status);

Thus, possible null-pointer dereferences may occur.

To fix these possible bugs, -EINVAL is returned when cb is NULL.

These bugs are found by a static analysis tool STCheck written by us.

---
 fs/nfsd/nfs4callback.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
fs/nfsd/nfs4callback.c +8 -3
index 397eb7820929..55949a158b6b 100644
@@ -516,7 +516,8 @@ static int nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp,
 		status = decode_cb_sequence4res(xdr, cb);
 		if (unlikely(status || cb->cb_seq_status))
 			return status;
-	}
+	} else
+		return -EINVAL;
 
 	return decode_cb_op_status(xdr, OP_CB_RECALL, &cb->cb_status);
 }
@@ -608,7 +609,9 @@ static int nfs4_xdr_dec_cb_layout(struct rpc_rqst *rqstp,
 		status = decode_cb_sequence4res(xdr, cb);
 		if (unlikely(status || cb->cb_seq_status))
 			return status;
-	}
+	} else
+		return -EINVAL;
+
 	return decode_cb_op_status(xdr, OP_CB_LAYOUTRECALL, &cb->cb_status);
 }
 #endif /* CONFIG_NFSD_PNFS */
@@ -667,7 +670,9 @@ static int nfs4_xdr_dec_cb_notify_lock(struct rpc_rqst *rqstp,
 		status = decode_cb_sequence4res(xdr, cb);
 		if (unlikely(status || cb->cb_seq_status))
 			return status;
-	}
+	} else
+		return -EINVAL;
+	
 	return decode_cb_op_status(xdr, OP_CB_NOTIFY_LOCK, &cb->cb_status);
 }
 
Signature · 1 line
2.17.0
#1 lore raw
Quoted 4 lines · Jia-Ju Bai

In nfs4_xdr_dec_cb_recall(), nfs4_xdr_dec_cb_layout() and nfs4_xdr_dec_cb_notify_lock(), there is an if statement to check whether cb is NULL.

Quoted 8 lines

When cb is NULL, the three functions all call:

    decode_cb_op_status(..., &cb->cb_status);

Thus, possible null-pointer dereferences may occur.

To fix these possible bugs, -EINVAL is returned when cb is NULL.

These bugs are found by a static analysis tool STCheck written by us.

Thanks! But I think actually the correct fix is just to remove the NULL checks entirely.

--b.

Quoted 44 lines
---
 fs/nfsd/nfs4callback.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
fs/nfsd/nfs4callback.c +8 -3
index 397eb7820929..55949a158b6b 100644
@@ -516,7 +516,8 @@ static int nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp,
 		status = decode_cb_sequence4res(xdr, cb);
 		if (unlikely(status || cb->cb_seq_status))
 			return status;
-	}
+	} else
+		return -EINVAL;
 
 	return decode_cb_op_status(xdr, OP_CB_RECALL, &cb->cb_status);
 }
@@ -608,7 +609,9 @@ static int nfs4_xdr_dec_cb_layout(struct rpc_rqst *rqstp,
 		status = decode_cb_sequence4res(xdr, cb);
 		if (unlikely(status || cb->cb_seq_status))
 			return status;
-	}
+	} else
+		return -EINVAL;
+
 	return decode_cb_op_status(xdr, OP_CB_LAYOUTRECALL, &cb->cb_status);
 }
 #endif /* CONFIG_NFSD_PNFS */
@@ -667,7 +670,9 @@ static int nfs4_xdr_dec_cb_notify_lock(struct rpc_rqst *rqstp,
 		status = decode_cb_sequence4res(xdr, cb);
 		if (unlikely(status || cb->cb_seq_status))
 			return status;
-	}
+	} else
+		return -EINVAL;
+	
 	return decode_cb_op_status(xdr, OP_CB_NOTIFY_LOCK, &cb->cb_status);
 }
 
Signature · 1 line
2.17.0
#2 lore raw
Quoted 2 lines · J. Bruce Fields

Thanks! But I think actually the correct fix is just to remove the NULL checks entirely.

So, something like the following (untested).--b.

commit 7ce38d2d8a66

Author: J. Bruce Fields <[email protected]>
Date:   Tue Jul 30 19:06:38 2019 -0400

nfsd: Remove unnecessary NULL checks

"cb" is never actually NULL in these functions.

On a quick skim of the history, they seem to have been there from the beginning. I'm not sure if they originally served a purpose.

fs/nfsd/nfs4callback.c +15 -20
index 397eb7820929..524111420b48 100644
@@ -512,11 +512,9 @@ static int nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp,
 	if (unlikely(status))
 		return status;
 
-	if (cb != NULL) {
-		status = decode_cb_sequence4res(xdr, cb);
-		if (unlikely(status || cb->cb_seq_status))
-			return status;
-	}
+	status = decode_cb_sequence4res(xdr, cb);
+	if (unlikely(status || cb->cb_seq_status))
+		return status;
 
 	return decode_cb_op_status(xdr, OP_CB_RECALL, &cb->cb_status);
 }
@@ -604,11 +602,10 @@ static int nfs4_xdr_dec_cb_layout(struct rpc_rqst *rqstp,
 	if (unlikely(status))
 		return status;
 
-	if (cb) {
-		status = decode_cb_sequence4res(xdr, cb);
-		if (unlikely(status || cb->cb_seq_status))
-			return status;
-	}
+	status = decode_cb_sequence4res(xdr, cb);
+	if (unlikely(status || cb->cb_seq_status))
+		return status;
+
 	return decode_cb_op_status(xdr, OP_CB_LAYOUTRECALL, &cb->cb_status);
 }
 #endif /* CONFIG_NFSD_PNFS */
@@ -663,11 +660,10 @@ static int nfs4_xdr_dec_cb_notify_lock(struct rpc_rqst *rqstp,
 	if (unlikely(status))
 		return status;
 
-	if (cb) {
-		status = decode_cb_sequence4res(xdr, cb);
-		if (unlikely(status || cb->cb_seq_status))
-			return status;
-	}
+	status = decode_cb_sequence4res(xdr, cb);
+	if (unlikely(status || cb->cb_seq_status))
+		return status;
+
 	return decode_cb_op_status(xdr, OP_CB_NOTIFY_LOCK, &cb->cb_status);
 }
 
@@ -759,11 +755,10 @@ static int nfs4_xdr_dec_cb_offload(struct rpc_rqst *rqstp,
 	if (unlikely(status))
 		return status;
 
-	if (cb) {
-		status = decode_cb_sequence4res(xdr, cb);
-		if (unlikely(status || cb->cb_seq_status))
-			return status;
-	}
+	status = decode_cb_sequence4res(xdr, cb);
+	if (unlikely(status || cb->cb_seq_status))
+		return status;
+
 	return decode_cb_op_status(xdr, OP_CB_OFFLOAD, &cb->cb_status);
 }
 /*