From patchwork Tue May 14 21:53:06 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 243849 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 91E492C00B2 for ; Wed, 15 May 2013 08:02:51 +1000 (EST) Received: from localhost ([::1]:48146 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UcNJ3-0001fE-Ol for incoming@patchwork.ozlabs.org; Tue, 14 May 2013 18:02:49 -0400 Received: from eggs.gnu.org ([208.118.235.92]:48296) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UcNCJ-00078L-0q for qemu-devel@nongnu.org; Tue, 14 May 2013 17:55:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UcNCH-0003v3-1D for qemu-devel@nongnu.org; Tue, 14 May 2013 17:55:50 -0400 Received: from mail-ia0-x234.google.com ([2607:f8b0:4001:c02::234]:51063) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UcNCG-0003uu-Ni; Tue, 14 May 2013 17:55:48 -0400 Received: by mail-ia0-f180.google.com with SMTP id l27so1261067iae.39 for ; Tue, 14 May 2013 14:55:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:sender:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references; bh=k1zOitqtTTgE9yWtZqTgXz6o3I51JGFtkZkDRN9tKh8=; b=VfhdXUDKVkI90wMoZip7QSzeMFx1+uHFaRQ4ixrZd8FsBKjgnLIsg9u/aQzgHrVIDl 7KYqvpsDUwtoEGX73pRfa9MTzGz5OevK8FSO1bcAW+muinzcCpe/vGFkKI8yIUwUwchd kQYfaVkjTfnNMk8DkxQSNsfPzUQ+5nwVoHxGdJzjEYEXq2gjsGodVQKB5vpFPRVyYDoM oA+RyGdS5oteF2h/b2tieeT6hrcLimSLIS7rZxrrxJTYpFfwoGj3H44+Hv60q3epamfl la2jr3FDaQWTLOH3sByx/eIdrMsLlCPqYCbzuF/yj9Wyq7nKog2oU4oZzPYd10jETRBG uP7w== X-Received: by 10.50.128.101 with SMTP id nn5mr3634844igb.82.1368568548070; Tue, 14 May 2013 14:55:48 -0700 (PDT) Received: from localhost (cpe-72-177-121-217.austin.res.rr.com. [72.177.121.217]) by mx.google.com with ESMTPSA id q3sm269201igw.0.2013.05.14.14.55.46 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Tue, 14 May 2013 14:55:47 -0700 (PDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Tue, 14 May 2013 16:53:06 -0500 Message-Id: <1368568392-2127-10-git-send-email-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1368568392-2127-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1368568392-2127-1-git-send-email-mdroth@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4001:c02::234 Cc: aliguori@us.ibm.com, qemu-stable@nongnu.org Subject: [Qemu-devel] [PATCH 09/15] qga: set umask 0077 when daemonizing (CVE-2013-2007) X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Laszlo Ersek The qemu guest agent creates a bunch of files with insecure permissions when started in daemon mode. For example: -rw-rw-rw- 1 root root /var/log/qemu-ga.log -rw-rw-rw- 1 root root /var/run/qga.state -rw-rw-rw- 1 root root /var/log/qga-fsfreeze-hook.log In addition, at least all files created with the "guest-file-open" QMP command, and all files created with shell output redirection (or otherwise) by utilities invoked by the fsfreeze hook script are affected. For now mask all file mode bits for "group" and "others" in become_daemon(). Temporarily, for compatibility reasons, stick with the 0666 file-mode in case of files newly created by the "guest-file-open" QMP call. Do so without changing the umask temporarily. Signed-off-by: Laszlo Ersek Signed-off-by: Anthony Liguori (cherry picked from commit c689b4f1bac352dcfd6ecb9a1d45337de0f1de67) Conflicts: qga/commands-posix.c *update includes to match stable Signed-off-by: Michael Roth --- qga/commands-posix.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++++-- qga/main.c | 2 +- 2 files changed, 117 insertions(+), 5 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 1c2aff3..08f3473 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -162,9 +162,122 @@ static GuestFileHandle *guest_file_handle_find(int64_t id, Error **err) return NULL; } +typedef const char * const ccpc; + +/* http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html */ +static const struct { + ccpc *forms; + int oflag_base; +} guest_file_open_modes[] = { + { (ccpc[]){ "r", "rb", NULL }, O_RDONLY }, + { (ccpc[]){ "w", "wb", NULL }, O_WRONLY | O_CREAT | O_TRUNC }, + { (ccpc[]){ "a", "ab", NULL }, O_WRONLY | O_CREAT | O_APPEND }, + { (ccpc[]){ "r+", "rb+", "r+b", NULL }, O_RDWR }, + { (ccpc[]){ "w+", "wb+", "w+b", NULL }, O_RDWR | O_CREAT | O_TRUNC }, + { (ccpc[]){ "a+", "ab+", "a+b", NULL }, O_RDWR | O_CREAT | O_APPEND } +}; + +static int +find_open_flag(const char *mode_str, Error **err) +{ + unsigned mode; + + for (mode = 0; mode < ARRAY_SIZE(guest_file_open_modes); ++mode) { + ccpc *form; + + form = guest_file_open_modes[mode].forms; + while (*form != NULL && strcmp(*form, mode_str) != 0) { + ++form; + } + if (*form != NULL) { + break; + } + } + + if (mode == ARRAY_SIZE(guest_file_open_modes)) { + error_setg(err, "invalid file open mode '%s'", mode_str); + return -1; + } + return guest_file_open_modes[mode].oflag_base | O_NOCTTY | O_NONBLOCK; +} + +#define DEFAULT_NEW_FILE_MODE (S_IRUSR | S_IWUSR | \ + S_IRGRP | S_IWGRP | \ + S_IROTH | S_IWOTH) + +static FILE * +safe_open_or_create(const char *path, const char *mode, Error **err) +{ + Error *local_err = NULL; + int oflag; + + oflag = find_open_flag(mode, &local_err); + if (local_err == NULL) { + int fd; + + /* If the caller wants / allows creation of a new file, we implement it + * with a two step process: open() + (open() / fchmod()). + * + * First we insist on creating the file exclusively as a new file. If + * that succeeds, we're free to set any file-mode bits on it. (The + * motivation is that we want to set those file-mode bits independently + * of the current umask.) + * + * If the exclusive creation fails because the file already exists + * (EEXIST is not possible for any other reason), we just attempt to + * open the file, but in this case we won't be allowed to change the + * file-mode bits on the preexistent file. + * + * The pathname should never disappear between the two open()s in + * practice. If it happens, then someone very likely tried to race us. + * In this case just go ahead and report the ENOENT from the second + * open() to the caller. + * + * If the caller wants to open a preexistent file, then the first + * open() is decisive and its third argument is ignored, and the second + * open() and the fchmod() are never called. + */ + fd = open(path, oflag | ((oflag & O_CREAT) ? O_EXCL : 0), 0); + if (fd == -1 && errno == EEXIST) { + oflag &= ~(unsigned)O_CREAT; + fd = open(path, oflag); + } + + if (fd == -1) { + error_setg_errno(&local_err, errno, "failed to open file '%s' " + "(mode: '%s')", path, mode); + } else { + qemu_set_cloexec(fd); + + if ((oflag & O_CREAT) && fchmod(fd, DEFAULT_NEW_FILE_MODE) == -1) { + error_setg_errno(&local_err, errno, "failed to set permission " + "0%03o on new file '%s' (mode: '%s')", + (unsigned)DEFAULT_NEW_FILE_MODE, path, mode); + } else { + FILE *f; + + f = fdopen(fd, mode); + if (f == NULL) { + error_setg_errno(&local_err, errno, "failed to associate " + "stdio stream with file descriptor %d, " + "file '%s' (mode: '%s')", fd, path, mode); + } else { + return f; + } + } + + close(fd); + } + } + + error_propagate(err, local_err); + return NULL; +} + int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err) { FILE *fh; + Error *local_err = NULL; int fd; int64_t ret = -1, handle; @@ -172,10 +285,9 @@ int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, E mode = "r"; } slog("guest-file-open called, filepath: %s, mode: %s", path, mode); - fh = fopen(path, mode); - if (!fh) { - error_setg_errno(err, errno, "failed to open file '%s' (mode: '%s')", - path, mode); + fh = safe_open_or_create(path, mode, &local_err); + if (local_err != NULL) { + error_propagate(err, local_err); return -1; } diff --git a/qga/main.c b/qga/main.c index 74ef788..028fceb 100644 --- a/qga/main.c +++ b/qga/main.c @@ -478,7 +478,7 @@ static void become_daemon(const char *pidfile) } } - umask(0); + umask(S_IRWXG | S_IRWXO); sid = setsid(); if (sid < 0) { goto fail;