git: a65e23fb8ab6 - main - kern_linker: Fix struct version mismatch

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Wed, 25 Nov 2026 17:14:21 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=a65e23fb8ab662c880f3d1eedfaa4659ef5f673d

commit a65e23fb8ab662c880f3d1eedfaa4659ef5f673d
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-11-25 17:13:34 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-11-25 17:13:34 +0000

    kern_linker: Fix struct version mismatch
    
    The kldstat(2) syscall supports two different structures, identified by
    their size, which the application stores in the version field before the
    call.  Since one is a prefix of the other, instead of calling different
    functions to fill each one in like modstat(2) does, we fill in the
    larger of the two and simply don't copy out the extra fields if the
    smaller one was requested.  As a side effect, we failed to fill out the
    version field in either case.  This causes application code that sets
    the version field once and calls kldstat(2) repeatedly to fail since the
    version field is now incorrect for subsequent calls, whereas the same
    pattern works fine for modstat(2) because it copies the same version
    value back out.  Fix this by setting the version field immediately
    before copying the struct out.
    
    While here, amend the manual pages for both kldstat(2) and modstat(2) to
    clarify that the version field is unchanged on return and remove bad
    advice about rebuilding world if either syscall returns EINVAL.
    
    MFC after:      1 week
    Reviewed by:    imp, kib
    Differential Revision:  https://reviews.freebsd.org/D61992
---
 lib/libsys/kldstat.2   | 15 +++------------
 lib/libsys/modstat.2   | 15 +++------------
 sys/kern/kern_linker.c |  4 +++-
 3 files changed, 9 insertions(+), 25 deletions(-)

diff --git a/lib/libsys/kldstat.2 b/lib/libsys/kldstat.2
index 111323a0da8d..ef77e53ba770 100644
--- a/lib/libsys/kldstat.2
+++ b/lib/libsys/kldstat.2
@@ -23,7 +23,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd March 28, 2012
+.Dd November 24, 2026
 .Dt KLDSTAT 2
 .Os
 .Sh NAME
@@ -55,12 +55,8 @@ struct kld_file_stat {
 .Ed
 .Bl -tag -width XXXaddress
 .It version
-This field is set to the size of the structure mentioned above by the code
-calling
-.Fn kldstat ,
-and not
-.Fn kldstat
-itself.
+This field must be set to the size of the structure mentioned above by
+the caller and is left unmodified on return.
 .It name
 The name of the file referred to by
 .Fa fileid .
@@ -94,11 +90,6 @@ The file was not found (probably not loaded).
 The version specified in the
 .Fa version
 field of stat is not the proper version.
-You would need to rebuild world, the
-kernel, or your application, if this error occurs, given that you did properly
-fill in the
-.Fa version
-field.
 .It Bq Er EFAULT
 There was a problem copying one, some, or all of the fields into
 .Fa stat
diff --git a/lib/libsys/modstat.2 b/lib/libsys/modstat.2
index cea7094cd586..faf8dee8331a 100644
--- a/lib/libsys/modstat.2
+++ b/lib/libsys/modstat.2
@@ -23,7 +23,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd July 15, 2008
+.Dd November 24, 2026
 .Dt MODSTAT 2
 .Os
 .Sh NAME
@@ -60,12 +60,8 @@ typedef union modspecific {
 .Ed
 .Bl -tag -width XXXaddress
 .It version
-This field is set to the size of the structure mentioned above by the code
-calling
-.Fn modstat ,
-and not
-.Fn modstat
-itself.
+This field must be set to the size of the structure mentioned above by
+the caller and is left unmodified on return.
 .It name
 The name of the module referred to by
 .Fa modid .
@@ -93,11 +89,6 @@ The module was not found (probably not loaded).
 The version specified in the
 .Fa version
 field of stat is not the proper version.
-You would need to rebuild world, the
-kernel, or your application, if this error occurs, given that you did properly
-fill in the
-.Fa version
-field.
 .It Bq Er EFAULT
 There was a problem copying one, some, or all of the fields into
 .Fa stat
diff --git a/sys/kern/kern_linker.c b/sys/kern/kern_linker.c
index 96fa8e193949..2dce141a9928 100644
--- a/sys/kern/kern_linker.c
+++ b/sys/kern/kern_linker.c
@@ -1464,8 +1464,10 @@ sys_kldstat(struct thread *td, struct kldstat_args *uap)
 
 	stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
 	error = kern_kldstat(td, uap->fileid, stat);
-	if (error == 0)
+	if (error == 0) {
+		stat->version = version;
 		error = copyout(stat, uap->stat, version);
+	}
 	free(stat, M_TEMP);
 	return (error);
 }