]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme: set file permission for keyfile to owner only
authorDaniel Wagner <dwagner@suse.de>
Thu, 24 Oct 2024 13:23:33 +0000 (15:23 +0200)
committerDaniel Wagner <wagi@monom.org>
Mon, 28 Oct 2024 18:38:51 +0000 (19:38 +0100)
Since this file contains secret enforce the read/write permission
limited to the owner only.

Signed-off-by: Daniel Wagner <dwagner@suse.de>
nvme.c

diff --git a/nvme.c b/nvme.c
index 9ba59bddd451e5f062838abf9587e086407fa811..d1107ca16f337350ffdb9121b8c4f12c70120eab 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -9480,6 +9480,7 @@ static int tls_key(int argc, char **argv, struct command *command, struct plugin
        const char *revoke = "Revoke key from the keyring.";
 
        _cleanup_file_ FILE *fd = NULL;
+       mode_t old_umask = 0;
        int cnt, err = 0;
 
        struct config {
@@ -9520,6 +9521,8 @@ static int tls_key(int argc, char **argv, struct command *command, struct plugin
                else
                        mode = "w";
 
+               old_umask = umask(0);
+
                fd = fopen(cfg.keyfile, mode);
                if (!fd) {
                        nvme_show_error("Cannot open keyfile %s, error %d",
@@ -9575,6 +9578,11 @@ static int tls_key(int argc, char **argv, struct command *command, struct plugin
                        printf("revoking key\n");
        }
 
+       if (old_umask != 0 && fd) {
+               umask(old_umask);
+               chmod(cfg.keyfile, 0600);
+       }
+
        return err;
 }