Skip to contentSkip to conversation

tomoyo: use vsnprintf() properly

PATCH In review Al Viro 2 participants 4 messages Aug 20, 2022 – Aug 21, 2022

Subsystems (3)
lore
Share & tools
lore

Al Viro

author maintainer
#0 lore raw

PATCH 1/2 tomoyo: use vsnprintf() properly

Idiomatic way to find how much space sprintf output would take is

	len = snprintf(NULL, 0, ...) + 1;
Once upon a time there'd been libc implementations that blew chunks
on that and somebody had come up with the following "cute" trick:
	len = snprintf((char *) &len, 1, ...) + 1;

for doing the same. However, that's unidiomatic, harder to follow *and* any such libc implementation would violate both C99 and POSIX (since 2001).
IOW, this kludge is best buried along with such libc implementations,
nevermind getting cargo-culted into newer code. Our vsnprintf() does not suffer that braindamage, TYVM.

---
 security/tomoyo/audit.c  | 2 +-
 security/tomoyo/common.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
security/tomoyo/audit.c +1 -1
index 023bedd9dfa3..7cf8fdbb29bf 100644
@@ -423,7 +423,7 @@ void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
 	int len;
 
 	va_start(args, fmt);
-	len = vsnprintf((char *) &len, 1, fmt, args) + 1;
+	len = vsnprintf(NULL, 0, fmt, args) + 1;
 	va_end(args);
 	va_start(args, fmt);
 	tomoyo_write_log2(r, len, fmt, args);
security/tomoyo/common.c +1 -1
index ff17abc96e5c..f4cd9b58b205 100644
@@ -2057,7 +2057,7 @@ int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
 	bool quota_exceeded = false;
 
 	va_start(args, fmt);
-	len = vsnprintf((char *) &len, 1, fmt, args) + 1;
+	len = vsnprintf(NULL, 0, fmt, args) + 1;
 	va_end(args);
 	/* Write /sys/kernel/security/tomoyo/audit. */
 	va_start(args, fmt);
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 2/2 tomoyo: struct path it might get from LSM callers won't have NULL dentry or mnt

---
 security/tomoyo/file.c     | 2 +-
 security/tomoyo/realpath.c | 9 ++-------
 2 files changed, 3 insertions(+), 8 deletions(-)
security/tomoyo/file.c +1 -1
index 1e6077568fde..8f3b90b6e03d 100644
@@ -717,7 +717,7 @@ int tomoyo_path_number_perm(const u8 type, const struct path *path,
 	int idx;
 
 	if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type])
-	    == TOMOYO_CONFIG_DISABLED || !path->dentry)
+	    == TOMOYO_CONFIG_DISABLED)
 		return 0;
 	idx = tomoyo_read_lock();
 	if (!tomoyo_get_realpath(&buf, path))
security/tomoyo/realpath.c +2 -7
index df4798980416..1c483ee7f93d 100644
@@ -240,11 +240,8 @@ char *tomoyo_realpath_from_path(const struct path *path)
 	char *name = NULL;
 	unsigned int buf_len = PAGE_SIZE / 2;
 	struct dentry *dentry = path->dentry;
-	struct super_block *sb;
+	struct super_block *sb = dentry->d_sb;
 
-	if (!dentry)
-		return NULL;
-	sb = dentry->d_sb;
 	while (1) {
 		char *pos;
 		struct inode *inode;
@@ -264,10 +261,8 @@ char *tomoyo_realpath_from_path(const struct path *path)
 		inode = d_backing_inode(sb->s_root);
 		/*
 		 * Get local name for filesystems without rename() operation
-		 * or dentry without vfsmount.
 		 */
-		if (!path->mnt ||
-		    (!inode->i_op->rename &&
+		if ((!inode->i_op->rename &&
 		     !(sb->s_type->fs_flags & FS_REQUIRES_DEV)))
 			pos = tomoyo_get_local_path(path->dentry, buf,
 						    buf_len - 1);
Signature · 1 line
2.30.2
maintainer Acked-by on v1 1/2
#3 lore raw
In reply to Al Viro
Quoted 5 lines · Al Viro

Idiomatic way to find how much space sprintf output would take is

	len = snprintf(NULL, 0, ...) + 1;
Once upon a time there'd been libc implementations that blew chunks
on that and somebody had come up with the following "cute" trick:
	len = snprintf((char *) &len, 1, ...) + 1;
Quoted 8 lines

for doing the same. However, that's unidiomatic, harder to follow *and* any such libc implementation would violate both C99 and POSIX (since 2001).
IOW, this kludge is best buried along with such libc implementations,
nevermind getting cargo-culted into newer code. Our vsnprintf() does not suffer that braindamage, TYVM.

Thank you. You can send this change via your tree if you like.