]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
lockd: Move nlm4svc_set_file_lock_range()
authorChuck Lever <chuck.lever@oracle.com>
Wed, 28 Jan 2026 15:19:34 +0000 (10:19 -0500)
committerChuck Lever <chuck.lever@oracle.com>
Mon, 30 Mar 2026 01:25:09 +0000 (21:25 -0400)
Both client-side and server-side NLMv4 code convert lock byte ranges
from the wire format (start, length) to the kernel's file_lock format
(start, end). The current nlm4svc_set_file_lock_range() performs this
conversion, but the "svc" prefix incorrectly suggests server-only use,
and client code must include server-internal headers to access it.

Rename to lockd_set_file_lock_range4() and relocate to the shared
lockd.h header, making it accessible to both client and server code.
This eliminates the need for client code to include xdr4.h, reducing
coupling between the XDR implementation files.

While relocating the function, add input validation: clamp the
starting offset to OFFSET_MAX before use. Without this, a malformed
lock request with off > OFFSET_MAX results in fl_start > fl_end,
violating file_lock invariants and potentially causing incorrect
lock conflict detection.

Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
fs/lockd/clnt4xdr.c
fs/lockd/lockd.h
fs/lockd/xdr4.c
fs/lockd/xdr4.h

index 61ee5fa6dfa460bd43ea6c24f75bc89d3c807bb9..c09e67765cac412fa751560faf670e4b7fb1bd47 100644 (file)
@@ -287,7 +287,7 @@ static int decode_nlm4_holder(struct xdr_stream *xdr, struct nlm_res *result)
        fl->c.flc_type  = exclusive != 0 ? F_WRLCK : F_RDLCK;
        p = xdr_decode_hyper(p, &l_offset);
        xdr_decode_hyper(p, &l_len);
-       nlm4svc_set_file_lock_range(fl, l_offset, l_len);
+       lockd_set_file_lock_range4(fl, l_offset, l_len);
        error = 0;
 out:
        return error;
index e73c6b34815490a6c11578bd71c2a92af0a2b4c7..ef6431b4cac0663c33860298825b1d1564f27d1d 100644 (file)
@@ -413,6 +413,31 @@ static inline int nlm_compare_locks(const struct file_lock *fl1,
             &&(fl1->c.flc_type  == fl2->c.flc_type || fl2->c.flc_type == F_UNLCK);
 }
 
+/**
+ * lockd_set_file_lock_range4 - set the byte range of a file_lock
+ * @fl: file_lock whose length fields are to be initialized
+ * @off: starting offset of the lock, in bytes
+ * @len: length of the byte range, in bytes, or zero
+ *
+ * The NLMv4 protocol represents lock byte ranges as (start, length),
+ * where length zero means "lock to end of file." The kernel's file_lock
+ * structure uses (start, end) representation. Convert from NLMv4 format
+ * to file_lock format, clamping the starting offset and treating
+ * arithmetic overflow as "lock to EOF."
+ */
+static inline void
+lockd_set_file_lock_range4(struct file_lock *fl, u64 off, u64 len)
+{
+       u64 clamped_off = (off > OFFSET_MAX) ? OFFSET_MAX : off;
+       s64 end = clamped_off + len - 1;
+
+       fl->fl_start = clamped_off;
+       if (len == 0 || end < 0)
+               fl->fl_end = OFFSET_MAX;
+       else
+               fl->fl_end = end;
+}
+
 extern const struct lock_manager_operations nlmsvc_lock_operations;
 
 #endif /* _LOCKD_LOCKD_H */
index f57d4881d5f1706cc1f84709e500cc9c682a56f3..dbbb2dfcb81bc58f6d4c80e5ba61543c285050e8 100644 (file)
@@ -34,17 +34,6 @@ loff_t_to_s64(loff_t offset)
        return res;
 }
 
-void nlm4svc_set_file_lock_range(struct file_lock *fl, u64 off, u64 len)
-{
-       s64 end = off + len - 1;
-
-       fl->fl_start = off;
-       if (len == 0 || end < 0)
-               fl->fl_end = OFFSET_MAX;
-       else
-               fl->fl_end = end;
-}
-
 /*
  * NLM file handles are defined by specification to be a variable-length
  * XDR opaque no longer than 1024 bytes. However, this implementation
@@ -91,7 +80,7 @@ svcxdr_decode_lock(struct xdr_stream *xdr, struct nlm_lock *lock)
 
        locks_init_lock(fl);
        fl->c.flc_type  = F_RDLCK;
-       nlm4svc_set_file_lock_range(fl, lock->lock_start, lock->lock_len);
+       lockd_set_file_lock_range4(fl, lock->lock_start, lock->lock_len);
        return true;
 }
 
index 7be318c0512bda2d6ee1fb737546fdc5601ef5fc..4ddf51a2e0eae6b67c13bc68e53d69f4430cd8d5 100644 (file)
@@ -15,7 +15,6 @@
 #define        nlm4_fbig               cpu_to_be32(NLM_FBIG)
 #define        nlm4_failed             cpu_to_be32(NLM_FAILED)
 
-void   nlm4svc_set_file_lock_range(struct file_lock *fl, u64 off, u64 len);
 bool   nlm4svc_decode_void(struct svc_rqst *rqstp, struct xdr_stream *xdr);
 bool   nlm4svc_decode_testargs(struct svc_rqst *rqstp, struct xdr_stream *xdr);
 bool   nlm4svc_decode_lockargs(struct svc_rqst *rqstp, struct xdr_stream *xdr);