<table><tr><td style="">fvogt added a comment.
</td><a style="text-decoration: none; padding: 4px 8px; margin: 0 8px 8px; float: right; color: #464C5C; font-weight: bold; border-radius: 3px; background-color: #F7F7F9; background-image: linear-gradient(to bottom,#fff,#f1f0f1); display: inline-block; border: 1px solid rgba(71,87,120,.2);" href="https://phabricator.kde.org/D5394" rel="noreferrer">View Revision</a></tr></table><br /><div><div><p>It is currently not possible to avoid a race condition as QSaveFile is broken and use of it is currently insecure as the file is created world-readable.<br />
It can be avoided by placing the file in a drwx------ directory, but that's more work than just using QTemporaryFile directly.</p>

<p>However, as far as I can tell it is impossible to make this secure with a simple rename() of a user-created file as the file descriptor<br />
will stay valid after renaming (!) so if you edit /etc/sudoers for example, any application can just open(, O_RDWR) the temporary<br />
file and write to it after the rename without any issues.</p>

<p>I'd suggest one of two ways:</p>

<ol class="remarkup-list">
<li class="remarkup-list-item">Checksum contents</li>
<li class="remarkup-list-item">Write contents into temporary file with rw-------</li>
<li class="remarkup-list-item">Start the privileged action:</li>
</ol>

<p>3.1. Read temporary file into memory<br />
3.2. Verify content with checksum<br />
3.3. Create new temporary file somewhere with rw------ and write content into it<br />
3.4. Change owner<br />
3.5. Change permissions<br />
3.6. Rename</p>

<ol class="remarkup-list" start="4">
<li class="remarkup-list-item">Remove temporary file</li>
</ol>

<ol class="remarkup-list">
<li class="remarkup-list-item">Start the privileged action, send file content directly</li>
</ol>

<p>2.1. Create new temporary file somewhere with rw------ and write content into it<br />
2.2. Change owner<br />
2.3. Change permissions<br />
2.4. Rename</p></div></div><br /><div><strong>REPOSITORY</strong><div><div>R39 KTextEditor</div></div></div><br /><div><strong>REVISION DETAIL</strong><div><a href="https://phabricator.kde.org/D5394" rel="noreferrer">https://phabricator.kde.org/D5394</a></div></div><br /><div><strong>To: </strong>martinkostolny, KTextEditor, fvogt<br /><strong>Cc: </strong>elvisangelaccio, aacid, ivan, lbeltrame, fvogt, apol, anthonyfieroni, cullmann, ltoscano, dhaumann, graesslin, davidedmundson, palant, kwrite-devel, dfaure, Frameworks, head7, kfunk, sars<br /></div>