git: f43ff3e15c8b - main - kern linker: Do not unload a module if it has dependants

From: Zhenlei Huang <zlei_at_FreeBSD.org>
Date: Tue, 26 Mar 2024 03:56:54 UTC
The branch main has been updated by zlei:

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

commit f43ff3e15c8b4b161ce09c8ab008abc4222db26b
Author:     Zhenlei Huang <zlei@FreeBSD.org>
AuthorDate: 2024-03-26 03:55:45 +0000
Commit:     Zhenlei Huang <zlei@FreeBSD.org>
CommitDate: 2024-03-26 03:55:45 +0000

    kern linker: Do not unload a module if it has dependants
    
    Despite the name, linker_file_unload() will drop a reference and return
    success when the module file has dependants, i.e. it has more than one
    reference. When user request to unload such modules then the kernel
    should reject unambiguously and immediately.
    
    PR:             274986
    Reviewed by:    dfr, dab, jhb
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D42527
---
 sys/kern/kern_linker.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/sys/kern/kern_linker.c b/sys/kern/kern_linker.c
index 1126bf967966..325dffbfedee 100644
--- a/sys/kern/kern_linker.c
+++ b/sys/kern/kern_linker.c
@@ -1280,6 +1280,8 @@ kern_kldunload(struct thread *td, int fileid, int flags)
 			printf("kldunload: attempt to unload file that was"
 			    " loaded by the kernel\n");
 			error = EBUSY;
+		} else if (lf->refs > 1) {
+			error = EBUSY;
 		} else {
 			lf->userrefs--;
 			error = linker_file_unload(lf, flags);