CVE-2016-6494 (published 2016-10-03T18:59Z)

The client in MongoDB uses world-readable permissions on .dbshell history files, which might allow local users to obtain sensitive information by reading these files.

Severity: LOW
CVSS v2 base score: 2.1 (AV:L/AC:L/Au:N/C:P/I:N/A:N)
CVSS v3 base score: 5.5, MEDIUM (CVSS:3.0/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:N/A:N)

Weakness information:
cwe_id: CWE-200
cwe_name: Exposure of Sensitive Information to an Unauthorized Actor
description: The product exposes sensitive information to an actor that is not explicitly authorized to have access to that information.
extended_description: 
	   There are many different kinds of mistakes that introduce information exposures. The severity of the error can range widely, depending on the context in which the product operates, the type of sensitive information that is revealed, and the benefits it may provide to an attacker.  Some kinds of sensitive information include:
       
         private, personal information, such as personal messages, financial data, health records, geographic location, or contact details
         system status and environment, such as the operating system and installed packages
		 business secrets and intellectual property
		 network status and configuration
		 the product's own code or internal state
         metadata, e.g. logging of connections or message headers
		 indirect information, such as a discrepancy between two internal operations that can be observed by an outsider
       
	    Information might be sensitive to different parties, each of which may have their own expectations for whether the information should be protected.  These parties include:
               
                  the product's own users
                  people or organizations whose information is created or used by the product, even if they are not direct product users
		  the product's administrators, including the admins of the system(s) and/or networks on which the product operates
                  the developer
               
	    Information exposures can occur in different ways:
               
                  the code explicitly inserts sensitive information into resources or messages that are intentionally made accessible to unauthorized actors, but should not contain the information - i.e., the information should have been "scrubbed" or "sanitized"
                  a different weakness or mistake indirectly inserts the sensitive information into resources, such as a web script error revealing the full system path of the program.
                  the code manages resources that intentionally contain sensitive information, but the resources are unintentionally made accessible to unauthorized actors. In this case, the information exposure is resultant - i.e., a different weakness enabled the access to the information in the first place.
               
	    It is common practice to describe any loss of confidentiality as an "information exposure," but this can lead to overuse of CWE-200 in CWE mapping. From the CWE perspective, loss of confidentiality is a technical impact that can arise from dozens of different weaknesses, such as insecure file permissions or out-of-bounds read.  CWE-200 and its lower-level descendants are intended to cover the mistakes that occur in behaviors that explicitly manage, store, transfer, or cleanse sensitive information.
         
         

Fix information:
repo_url: https://github.com/mongodb/mongo
commit_details: repo_url: https://github.com/mongodb/mongo
author: Kevin Pulo
author_date: 2016-09-06 11:44:20+10:00
msg: SERVER-25335 avoid group and other permissions when creating .dbshell history file
repository: repo_url: https://github.com/mongodb/mongo
repo_name: mongodb/mongo
description: The MongoDB Database
repo_language: C++
owner: mongodb
file_changes: filename: shell_history.js
change_type: ModificationType.ADD
diff: @@ -0,0 +1,103 @@
+// Test that when running the shell for the first time creates the ~/.dbshell file, and it has
+// appropriate permissions (where relevant).
+
+(function() {
+    "use strict";
+
+    // Use dataPath because it includes the trailing "/" or "\".
+    var tmpHome = MongoRunner.dataPath;
+    // Ensure it exists and is a dir (eg. if running without resmoke.py and /data/db doesn't exist).
+    mkdir(tmpHome);
+    removeFile(tmpHome + ".dbshell");
+
+    var args = [];
+    var cmdline = "mongo --nodb";
+    var redirection = "";
+    var env = {};
+    if (_isWindows()) {
+        args.push("cmd.exe");
+        args.push("/c");
+
+        // Input is set to NUL.  The output must also be redirected to NUL, otherwise running the
+        // jstest manually has strange terminal IO behaviour.
+        redirection = "< NUL > NUL";
+
+        // USERPROFILE set to the tmp homedir.
+        // Since NUL is a character device, isatty() will return true, which means that .mongorc.js
+        // will be created in the HOMEDRIVE + HOMEPATH location, so we must set them also.
+        if (tmpHome.match("^[a-zA-Z]:")) {
+            var tmpHomeDrive = tmpHome.substr(0, 2);
+            var tmpHomePath = tmpHome.substr(2);
+        } else {
+            var _pwd = pwd();
+            assert(_pwd.match("^[a-zA-Z]:"), "pwd must include drive");
+            var tmpHomeDrive = _pwd.substr(0, 2);
+            var tmpHomePath = tmpHome;
+        }
+        env = {USERPROFILE: tmpHome, HOMEDRIVE: tmpHomeDrive, HOMEPATH: tmpHomePath};
+
+    } else {
+        args.push("sh");
+        args.push("-c");
+
+        // Use the mongo shell from the current dir, same as resmoke.py does.
+        // Doesn't handle resmoke's --mongo= option.
+        cmdline = "./" + cmdline;
+
+        // Set umask to 0 prior to running the shell.
+        cmdline = "umask 0 ; " + cmdline;
+
+        // stdin is /dev/null.
+        redirection = "< /dev/null";
+
+        // HOME set to the tmp homedir.
+        if (!tmpHome.startsWith("/")) {
+            tmpHome = pwd() + "/" + tmpHome;
+        }
+        env = {HOME: tmpHome};
+    }
+
+    // Add redirection to cmdline, and add cmdline to args.
+    cmdline += " " + redirection;
+    args.push(cmdline);
+    jsTestLog("Running args:\n    " + tojson(args) + "\nwith env:\n    " + tojson(env));
+    var pid = _startMongoProgram({args, env});
+    var rc = waitProgram(pid);
+
+    assert.eq(rc, 0);
+
+    var files = listFiles(tmpHome);
+    jsTestLog(tojson(files));
+
+    var findFile = function(baseName) {
+        for (var i = 0; i < files.length; i++) {
+            if (files[i].baseName === baseName) {
+                return files[i];
+            }
+        }
+        return undefined;
+    };
+
+    var targetFile = ".dbshell";
+    var file = findFile(targetFile);
+
+    assert.neq(typeof(file), "undefined", targetFile + " should exist, but it doesn't");
+    assert.eq(file.isDirectory, false, targetFile + " should not be a directory, but it is");
+    assert.eq(file.size, 0, targetFile + " should be empty, but it isn't");
+
+    if (!_isWindows()) {
+        // On Unix, check that the file has the correct mode (permissions).
+        // The shell has no way to stat a file.
+        // There is no stat utility in POSIX.
+        // `ls -l` is POSIX, so this is the best that we have.
+        // Check for exactly "-rw-------".
+        clearRawMongoProgramOutput();
+        var rc = runProgram("ls", "-l", file.name);
+        assert.eq(rc, 0);
+        var output = rawMongoProgramOutput();
+        var fields = output.split(" ");
+        // First field is the prefix, second field is the `ls -l` permissions.
+        assert.eq(fields[1], "-rw-------", targetFile + " has bad permissions");
+    }
+
+})();

programming_language: JavaScript
filename: linenoise.cpp
change_type: ModificationType.MODIFY
diff: @@ -2762,7 +2762,17 @@ int linenoiseHistorySetMaxLen(int len) {
 /* Save the history in the specified file. On success 0 is returned
  * otherwise -1 is returned. */
 int linenoiseHistorySave(const char* filename) {
-    FILE* fp = fopen(filename, "wt");
+    FILE* fp;
+#if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
+    int fd = open(filename, O_CREAT, S_IRUSR | S_IWUSR);
+    if (fd == -1) {
+        // report errno somehow?
+        return -1;
+    }
+    fp = fdopen(fd, "wt");
+#else
+    fp = fopen(filename, "wt");
+#endif  // _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
     if (fp == NULL) {
         return -1;
     }
@@ -2772,7 +2782,7 @@ int linenoiseHistorySave(const char* filename) {
             fprintf(fp, "%s\n", history[j]);
         }
     }
-    fclose(fp);
+    fclose(fp);  // Also causes fd to be closed.
     return 0;
 }
 

programming_language: C++