🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: muc.lists.netbsd.tech.kern
Article #6

Re: compat_linux: open: Supporting O_NOATIME

#6
From: Aiman Najjar
Date: Sun, 18 Oct 2026 19:33
147 lines
7030 bytes
Hi tech-kern@,

I've prepared a patch for this below, BUT, in retrospect, I am not sure how 
consequential this fix is. The privilege check exists only to prevent 
processes from bypassing file access time "atime" update if they don't 
own the file or are not root, but we don't plan to offer that behavior 
anyway, i.e. atime will always be updated regardless of whether a 
process passes or fails the checks.

In other words, the patch is mostly cosmetic for purposes of passing the 
LTP test. With that in mind, I came up with these available 
options based on my research:

1. Do nothing, Linux binaries that pass O_NOATIME will still work. 
Unless a program uses this API to validate ownership of files or root 
privileges (unlikely and weird), program correctness should not be 
affected.

2. Implement a naive check. My patch does that, it checks file ownership 
when O_NOATIME is set, then delegates to the native sys_open(at) 
implementation. This suffers from a potential TOCTOU, but keeps the 
compat_linux layer a thin shim. It has performance implications as it 
will resolve the path twice, which would be even worse for backup software 
than doing nothing.

3. Implement a more robust check: Based on my research, it seems a 
MODULE_HOOK is the right way to add compatibility code inside the 
native sys_open(at) path. This would solve issues in #2 but would turn 
compat_linux into more than a translation layer, which does not seem to 
be worth it in this case.

4. Maybe there are other options that don't suffer from issues in #2 and 
still get us to pass the LTP test, would be happy to look into those 
based on any feedback.

In closing, I did not actually submit a patch to GNATS as my personal 
assessment is that option #1 (doing nothing) is probably better, but 
would appreciate your comments otherwise, regardless, it has been a very 
useful learning exercise, I appreciated the well-written kernel 
developer manual pages :)


diff --git a/sys/compat/linux/common/linux_file.c b/sys/compat/linux/common/linux_file.c
index 66b95278e..42a79491a 100644
--- a/sys/compat/linux/common/linux_file.c
+++ b/sys/compat/linux/common/linux_file.c
@@ -233,10 +233,38 @@ linux_sys_open(struct lwp *l, const struct linux_sys_open_args *uap,
 		syscallarg(int) flags;
 		syscallarg(linux_umode_t) mode;
 	} */
-	int error, fl;
+	int error, fl, lflags, uid;
 	struct sys_open_args boa;
+	struct pathbuf *pb;
+	struct nameidata nd;
+	struct vattr va;
 
 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
+	lflags = SCARG(uap, flags);
+	uid = kauth_cred_geteuid(l->l_cred);
+
+	if (lflags & LINUX_O_NOATIME) {
+		error = pathbuf_copyin(SCARG(uap, path), &pb);
+		if (error)
+			return error;
+
+		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, pb);
+		if ((error = namei(&nd)) == 0) {
+			if ((error = VOP_GETATTR(nd.ni_vp, &va, l->l_cred)))
+				goto out;
+
+			if (uid != va.va_uid &&
+			    kauth_authorize_generic(l->l_cred,
+				KAUTH_GENERIC_ISSUSER, NULL) != 0) {
+				error = EPERM;
+			}
+		out:
+			vput(nd.ni_vp);
+		}
+		pathbuf_destroy(pb);
+		if (error)
+			return error;
+	}
 
 	memset(&boa, 0, sizeof(boa));
 	SCARG(&boa, path) = SCARG(uap, path);
@@ -260,10 +288,55 @@ linux_sys_openat(struct lwp *l, const struct linux_sys_openat_args *uap,
 		syscallarg(int) flags;
 		syscallarg(linux_umode_t) mode;
 	} */
-	int error, fl;
+	int error, fl, lflags, uid;
 	struct sys_openat_args boa;
+	struct pathbuf *pb;
+	struct nameidata nd;
+	struct vattr va;
+	struct vnode *dvp = NULL;
+	const char *pathstring = NULL;
+	file_t *dfp = NULL;
 
 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
+	lflags = SCARG(uap, flags);
+	uid = kauth_cred_geteuid(l->l_cred);
+
+	if (lflags & LINUX_O_NOATIME) {
+		error = pathbuf_copyin(SCARG(uap, path), &pb);
+		if (error)
+			return error;
+
+		pathstring = pathbuf_stringcopy_get(pb);
+		if (SCARG(uap, fd) != AT_FDCWD && pathstring[0] != '/') {
+			if ((error = fd_getvnode(SCARG(uap, fd), &dfp)) != 0)
+				goto out2;
+			dvp = dfp->f_vnode;
+		}
+
+		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, pb);
+		if (dvp != NULL)
+			NDAT(&nd, dvp);
+
+		if ((error = namei(&nd)) == 0) {
+			if ((error = VOP_GETATTR(nd.ni_vp, &va, l->l_cred)))
+				goto out1;
+
+			if (uid != va.va_uid &&
+			    kauth_authorize_generic(l->l_cred,
+				KAUTH_GENERIC_ISSUSER, NULL) != 0) {
+				error = EPERM;
+			}
+out1:
+			vput(nd.ni_vp);
+		}
+		if (dvp != NULL)
+			fd_putfile(SCARG(uap, fd));
+out2:
+		pathbuf_stringcopy_put(pb, pathstring);
+		pathbuf_destroy(pb);
+		if (error)
+			return error;
+	}
 
 	memset(&boa, 0, sizeof(boa));
 	SCARG(&boa, fd) = SCARG(uap, fd);

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Technical Details
Message-ID: <atUeUSbX4OCfFunH@nixos>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!2.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!news.space.net!news.muc.de!.POSTED.news.muc.de!not-for-mail
References: <64843d8a-4319-48b0-b7f8-82927b43d402@hurranet.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!2.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!news.space.net!news.muc.de!.POSTED.news.muc.de!not-for-mail
From: Aiman Najjar <aiman.najjar@hurranet.com>
Newsgroups: muc.lists.netbsd.tech.kern
Subject: Re: compat_linux: open: Supporting O_NOATIME
Date: Sun, 18 Oct 2026 15:33:11 -0400
Organization: Newsgate at muc.de e.V.
Sender: tech-kern-owner@NetBSD.org
Approved: news-admin@muc.de
Distribution: world
Message-ID: <atUeUSbX4OCfFunH@nixos>
References: <64843d8a-4319-48b0-b7f8-82927b43d402@hurranet.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Injection-Info: news.muc.de; posting-host="news.muc.de:193.149.48.2";
	logging-data="26502"; mail-complaints-to="news-admin@muc.de"
Authentication-Results: mail.netbsd.org (amavisd-new);
	dkim=pass (2048-bit key) header.d=hurranet.com
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hurranet.com;
	s=MBO0001; t=1792351993;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:mime-version:mime-version:content-type:content-type:
	 in-reply-to:in-reply-to:references:references;
	bh=QduIRMKbMA1ydhD47cQcmvrGwOX3W6kl+vslZIpdl9o=;
	b=ApiONe3kRrOAm1o8AEGbIPiG/TIrJtrAMAJd6gq5X4rqHz66SKs++Y+99MnH9jSE3ycj/I
	S9q4FLB9zxNbhiY2o5WUVDJPTdqquSLAk+Bdz1SiCTO3p+9A2Vu1uRZfz/HK50/klh/37/
	93EowrvXdeFs64U8WDvn5DLudeQ2SMjt8e9VSyBjQYRsWWKmBReRxA8bNFOJ1PawgY7w44
	J9mfhOdVOJc+nP0SGfu5dmwdmOJTADO+4BkBqUY51z2lCNqLDPpYNKqzLe2IgXaxxm265H
	1KeGuUcNcMNM3Rmlbil4JwzYyHnABlMjMMbq1hSUIoeIjz3UnDscBxdPAxz7Ug==
Authentication-Results: outgoing_mbo_mout;
	dkim=none;
	spf=pass (outgoing_mbo_mout: domain of aiman.najjar@hurranet.com designates 2001:67c:2050:b231:465::202 as permitted sender) smtp.mailfrom=aiman.najjar@hurranet.com
Content-Disposition: inline
In-Reply-To: <64843d8a-4319-48b0-b7f8-82927b43d402@hurranet.com>
X-Rspamd-Queue-Id: 4j783Y4X4LzKp4k
Precedence: bulk
X-Newsgate-Id: f02c70b8849f+
X-No-Archive: Yes