From 61baa23e48be4c41d61eed78680d298ef079a620 Mon Sep 17 00:00:00 2001 From: Jens Remus Date: Mon, 3 Dec 2018 14:09:13 +0100 Subject: [PATCH] zdev: Fix reporting of read-only sysfs attributes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since commit d54213886815 ("zdev: use libutil provided path functions") lszdev fails to correctly report most read-only sysfs attributes as such in its detailed information output (options -i or -ii). This is because the libutil function util_path_is_writable() is not equivalent to the former function file_writable(). util_path_is_writable() always returns true (the underlying function access() indicates the file is writable), if the path exists and the user is root, regardless of the path's effective access permissions. This is correct for most use cases, as root can effectively read/write any path regardless of its effective access permissions). util_path_is_writable() behaves exactly like Bash -w in this regard. The former function file_writable() examined the file's access permissions to check if one or more of user, group, and other have write permission. For lszdev it is irrelevant whether a file is effectively writable by the current user. Instead it needs to determine whether sysfs attributes access permissions contain write access in any form. If not it lists those sysfs attributes in a separate read-only section. Use the new function util_path_is_readonly_file(), which does explicitly check the file's effective read and write access permissions. It behaves similar to the former function file_writable(), except that it does test whether any of user, group, and other have read permission. Fixes: d54213886815 ("zdev: use libutil provided path functions") Signed-off-by: Jens Remus Reviewed-by: Peter Oberparleiter Signed-off-by: Jan Höppner --- zdev/src/device.c | 5 +++-- zdev/src/module.c | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/zdev/src/device.c b/zdev/src/device.c index ea15abca..d17f7af4 100644 --- a/zdev/src/device.c +++ b/zdev/src/device.c @@ -1,7 +1,7 @@ /* * zdev - Modify and display the persistent configuration of devices * - * Copyright IBM Corp. 2016, 2017 + * Copyright IBM Corp. 2016, 2019 * * s390-tools is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -10,6 +10,7 @@ #include #include #include +#include #include "lib/util_path.h" @@ -521,7 +522,7 @@ void device_read_active_settings(struct device *dev, read_scope_t scope) s = setting_list_apply_actual(dev->active.settings, a, name, value); if (link || (scope == scope_all && - !util_path_is_writable(path))) + util_path_is_readonly_file("%s", path))) s->readonly = 1; if (link) free(link); diff --git a/zdev/src/module.c b/zdev/src/module.c index 09acb86e..a8e1bbb7 100644 --- a/zdev/src/module.c +++ b/zdev/src/module.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "lib/util_path.h" @@ -259,7 +260,7 @@ bool module_set_params(const char *mod, struct setting_list *settings) struct setting *s; char *path; const char *value; - bool result; + bool readonly; exit_code_t rc; /* First check if all modified settings can be applied this way. */ @@ -277,9 +278,9 @@ bool module_set_params(const char *mod, struct setting_list *settings) return false; } path = path_get_sys_module_param(mod, s->name); - result = util_path_is_writable(path); + readonly = util_path_is_readonly_file("%s", path); free(path); - if (!result) { + if (readonly) { /* Sysfs file is not writable. */ return false; }