Bug 1433982 - Make nsAtomicFileOutputStream::DoOpen() fail if the file is read-only. r=glandium draft
authorNicholas Nethercote <nnethercote@mozilla.com>
Tue, 30 Jan 2018 17:20:06 +1100
changeset 748658 771c16f70b8306a3c4b03553e993d9c47457e3f0
parent 748657 bb9188d53db57e5b72214e8e984fc5ad7a6db2dc
push id97221
push usernnethercote@mozilla.com
push dateTue, 30 Jan 2018 06:20:22 +0000
reviewersglandium
bugs1433982
milestone60.0a1
Bug 1433982 - Make nsAtomicFileOutputStream::DoOpen() fail if the file is read-only. r=glandium This means we don't leave behind prefs-<n>.js files when prefs.js is read-only. MozReview-Commit-ID: LTJl14aCtPB
netwerk/base/nsFileStreams.cpp
--- a/netwerk/base/nsFileStreams.cpp
+++ b/netwerk/base/nsFileStreams.cpp
@@ -805,16 +805,22 @@ nsAtomicFileOutputStream::DoOpen()
     }
 
     if (NS_SUCCEEDED(rv) && mTargetFileExists) {
         uint32_t origPerm;
         if (NS_FAILED(file->GetPermissions(&origPerm))) {
             NS_ERROR("Can't get permissions of target file");
             origPerm = mOpenParams.perm;
         }
+
+        // Abort if |file| is not writable; it won't work as an output stream.
+        if (!(origPerm & 0200)) {
+            return NS_ERROR_FILE_ACCESS_DENIED;
+        }
+
         // XXX What if |perm| is more restrictive then |origPerm|?
         // This leaves the user supplied permissions as they were.
         rv = tempResult->CreateUnique(nsIFile::NORMAL_FILE_TYPE, origPerm);
     }
     if (NS_SUCCEEDED(rv)) {
         // nsFileOutputStream::DoOpen will work on the temporary file, so we
         // prepare it and place it in mOpenParams.localFile.
         mOpenParams.localFile = tempResult;