<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.kde.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.6"
          urlbase="https://bugs.kde.org/"
          
          maintainer="sysadmin@kde.org"
>

    <bug>
          <bug_id>103331</bug_id>
          
          <creation_ts>2005-04-06 00:33:59 +0000</creation_ts>
          <short_desc>Group should be preserved when making backup</short_desc>
          <delta_ts>2026-02-06 17:58:52 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>3</classification_id>
          <classification>Frameworks and Libraries</classification>
          <product>frameworks-kio</product>
          <component>general</component>
          <version>unspecified</version>
          <rep_platform>openSUSE</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>NOR</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter>bjoernv</reporter>
          <assigned_to name="Martin Koller">martin</assigned_to>
          <cc>cfeck</cc>
    
    <cc>christoph</cc>
    
    <cc>faure</cc>
    
    <cc>kdedev</cc>
    
    <cc>kdelibs-bugs-null</cc>
    
    <cc>martin</cc>
    
    <cc>maxy</cc>
    
    <cc>nate</cc>
          
          <cf_commitlink>https://commits.kde.org/kio/d252f7958633f3374e77e1cc52f11c534ca55219</cf_commitlink>
          <cf_versionfixedin></cf_versionfixedin>
          <cf_sentryurl></cf_sentryurl>
          <votes>0</votes>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>332134</commentid>
    <comment_count>0</comment_count>
    <who name="">bjoernv</who>
    <bug_when>2005-04-06 00:33:59 +0000</bug_when>
    <thetext>Version:            (using KDE KDE 3.4.0)
Installed from:    SuSE RPMs
OS:                Linux

When kate creates backup files during saving, this files may have more permissions than the original file. This can be a security problem, if the original file contains sensitive information.

The following script show the problem. The user uses a umask of 0022 and creates a file with permissions 600. After editing and saving the file with kate, the backup file has permission 644.

I suggest, that the backup files should be created with the same permissions like the original.

Sample script:
--------------

/tmp/dir $ umask
0022
/tmp/dir $ touch afile.txt
/tmp/dir $ ls -l
total 0
-rw-r--r--  1 bv bv 0 Apr  6 00:27 afile.txt
/tmp/dir $ chmod 600 afile.txt
/tmp/dir $ ls -l
total 0
-rw-------  1 bv bv 0 Apr  6 00:27 afile.txt
/tmp/dir $ kate afile.txt 
/tmp/dir $ ls -l
total 4
-rw-------  1 bv bv 8 Apr  6 00:27 afile.txt
-rw-r--r--  1 bv bv 0 Apr  6 00:27 afile.txt~
/tmp/dir $</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>332139</commentid>
    <comment_count>1</comment_count>
    <who name="">bjoernv</who>
    <bug_when>2005-04-06 00:38:20 +0000</bug_when>
    <thetext>Kwrite has the same problem: see Bug #103333</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>332661</commentid>
    <comment_count>2</comment_count>
    <who name="Christoph Cullmann">christoph</who>
    <bug_when>2005-04-07 18:47:05 +0000</bug_when>
    <thetext>*** Bug 103333 has been marked as a duplicate of this bug. ***</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>332663</commentid>
    <comment_count>3</comment_count>
    <who name="Christoph Cullmann">christoph</who>
    <bug_when>2005-04-07 18:51:40 +0000</bug_when>
    <thetext>CVS commit by cullmann: 

security fix: use right permissions for the backup files
if the filepermissions of the original are not accessable, why ever, use 0600 as fallback
dominik: please test and backport
BUG:103331


  M +31 -5     katedocument.cpp   1.818


--- kdelibs/kate/part/katedocument.cpp  #1.817:1.818
@@ -45,4 +45,6 @@
 #include &lt;kio/job.h&gt;
 #include &lt;kio/netaccess.h&gt;
+#include &lt;kio/kfileitem.h&gt;
+
 
 #include &lt;kparts/event.h&gt;
@@ -2431,13 +2433,37 @@ bool KateDocument::openFile(KIO::Job * j
 bool KateDocument::save()
 {
-  // FIXME reorder for efficiency, prompt user in case of failure
   bool l ( url().isLocalFile() );
-  if ( ( ( l &amp;&amp; config()-&gt;backupFlags() &amp; KateDocumentConfig::LocalFiles ) ||
-         ( ! l &amp;&amp; config()-&gt;backupFlags() &amp; KateDocumentConfig::RemoteFiles ) )
-       &amp;&amp; isModified() ) {
+
+  if ( ( l &amp;&amp; config()-&gt;backupFlags() &amp; KateDocumentConfig::LocalFiles )
+       || ( ! l &amp;&amp; config()-&gt;backupFlags() &amp; KateDocumentConfig::RemoteFiles ) )
+  {
     KURL u( url() );
     u.setFileName( config()-&gt;backupPrefix() + url().fileName() + config()-&gt;backupSuffix() );
-    if ( ! KIO::NetAccess::upload( url().path(), u, kapp-&gt;mainWidget() ) )
+
+    kdDebug () &lt;&lt; &quot;backup src file name: &quot; &lt;&lt; url() &lt;&lt; endl;
+    kdDebug () &lt;&lt; &quot;backup dst file name: &quot; &lt;&lt; u &lt;&lt; endl;
+
+    // get the right permissions, start with safe default
+    mode_t  perms = 0600;
+    KIO::UDSEntry fentry;
+    if (KIO::NetAccess::stat (url(), fentry, kapp-&gt;mainWidget()))
+    {
+      kdDebug () &lt;&lt; &quot;stating succesfull: &quot; &lt;&lt; url() &lt;&lt; endl;
+      KFileItem item (fentry, url());
+      perms = item.permissions();
+    }
+
+    // first del existing file if any, than copy over the file we have
+    // failure if a: the existing file could not be deleted, b: the file could not be copied
+    if ( (!KIO::NetAccess::exists( u, false, kapp-&gt;mainWidget() ) || KIO::NetAccess::del( u, kapp-&gt;mainWidget() ))
+          &amp;&amp; KIO::NetAccess::file_copy( url(), u, perms, true, false, kapp-&gt;mainWidget() ) )
+    {
+      kdDebug(13020)&lt;&lt;&quot;backing up successfull (&quot;&lt;&lt;url().prettyURL()&lt;&lt;&quot; -&gt; &quot;&lt;&lt;u.prettyURL()&lt;&lt;&quot;)&quot;&lt;&lt;endl;
+    }
+    else
+    {
       kdDebug(13020)&lt;&lt;&quot;backing up failed (&quot;&lt;&lt;url().prettyURL()&lt;&lt;&quot; -&gt; &quot;&lt;&lt;u.prettyURL()&lt;&lt;&quot;)&quot;&lt;&lt;endl;
+      // FIXME: notify user for real ;)
+    }
   }
 
</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>332728</commentid>
    <comment_count>4</comment_count>
    <who name="Dominik Haumann">dhaumann</who>
    <bug_when>2005-04-07 23:37:56 +0000</bug_when>
    <thetext>CVS commit by dhaumann: 

cullmann-backport: security fix: use right permissions for the backup files
if the filepermissions of the original are not accessable, why ever, use 0600 as fallback
CCBUG:103331


  M +31 -5     katedocument.cpp   1.796.2.5


--- kdelibs/kate/part/katedocument.cpp  #1.796.2.4:1.796.2.5
@@ -46,4 +46,6 @@
 #include &lt;kio/job.h&gt;
 #include &lt;kio/netaccess.h&gt;
+#include &lt;kio/kfileitem.h&gt;
+
 
 #include &lt;kparts/event.h&gt;
@@ -2750,13 +2752,37 @@ bool KateDocument::openFile(KIO::Job * j
 bool KateDocument::save()
 {
-  // FIXME reorder for efficiency, prompt user in case of failure
   bool l ( url().isLocalFile() );
-  if ( ( ( l &amp;&amp; config()-&gt;backupFlags() &amp; KateDocumentConfig::LocalFiles ) ||
-         ( ! l &amp;&amp; config()-&gt;backupFlags() &amp; KateDocumentConfig::RemoteFiles ) )
-       &amp;&amp; isModified() ) {
+
+  if ( ( l &amp;&amp; config()-&gt;backupFlags() &amp; KateDocumentConfig::LocalFiles )
+       || ( ! l &amp;&amp; config()-&gt;backupFlags() &amp; KateDocumentConfig::RemoteFiles ) )
+  {
     KURL u( url() );
     u.setFileName( config()-&gt;backupPrefix() + url().fileName() + config()-&gt;backupSuffix() );
-    if ( ! KIO::NetAccess::upload( url().path(), u, kapp-&gt;mainWidget() ) )
+
+    kdDebug () &lt;&lt; &quot;backup src file name: &quot; &lt;&lt; url() &lt;&lt; endl;
+    kdDebug () &lt;&lt; &quot;backup dst file name: &quot; &lt;&lt; u &lt;&lt; endl;
+
+    // get the right permissions, start with safe default
+    mode_t  perms = 0600;
+    KIO::UDSEntry fentry;
+    if (KIO::NetAccess::stat (url(), fentry, kapp-&gt;mainWidget()))
+    {
+      kdDebug () &lt;&lt; &quot;stating succesfull: &quot; &lt;&lt; url() &lt;&lt; endl;
+      KFileItem item (fentry, url());
+      perms = item.permissions();
+    }
+
+    // first del existing file if any, than copy over the file we have
+    // failure if a: the existing file could not be deleted, b: the file could not be copied
+    if ( (!KIO::NetAccess::exists( u, false, kapp-&gt;mainWidget() ) || KIO::NetAccess::del( u, kapp-&gt;mainWidget() ))
+          &amp;&amp; KIO::NetAccess::file_copy( url(), u, perms, true, false, kapp-&gt;mainWidget() ) )
+    {
+      kdDebug(13020)&lt;&lt;&quot;backing up successfull (&quot;&lt;&lt;url().prettyURL()&lt;&lt;&quot; -&gt; &quot;&lt;&lt;u.prettyURL()&lt;&lt;&quot;)&quot;&lt;&lt;endl;
+    }
+    else
+    {
       kdDebug(13020)&lt;&lt;&quot;backing up failed (&quot;&lt;&lt;url().prettyURL()&lt;&lt;&quot; -&gt; &quot;&lt;&lt;u.prettyURL()&lt;&lt;&quot;)&quot;&lt;&lt;endl;
+      // FIXME: notify user for real ;)
+    }
   }
 
</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>501956</commentid>
    <comment_count>5</comment_count>
    <who name="">bjoernv</who>
    <bug_when>2007-01-16 14:27:11 +0000</bug_when>
    <thetext>I reopened this bug because there is still a situation where the backup file may have too much permissions. I found the problem in Kate and Kwrite (KDE 3.5.5).

Currently Kate copies the permissions from the original file to the backup file. 

There is a potential problem if the original file is not owned by the standard group of the user. Kate creates the backup file with the standard group. Two files with the same permission bits but with other owners or groups can have different effective permissions. 

For fixing the problem there are two things to consider:

1) Kate should try to change the owner and group of the backup file according to the original file. 
2) chown and chmod may fail. In this case Kate should copy the usually lower permissions from the other/world entry to the group entry. 

I can recommend Gedit for example code: http://svn.gnome.org/svn/gedit/tags/GEDIT_2_16_2/gedit/gedit-document-saver.c</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>502178</commentid>
    <comment_count>6</comment_count>
    <who name="David Faure">faure</who>
    <bug_when>2007-01-17 19:10:08 +0000</bug_when>
    <thetext>Retitling. Indeed the group should be preserved when making a backup file.
This needs fixing at several levels in KIO, pretty much like all the places where the modification time is preserved when copying a file.
Added to my TODO list for kde4.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>502181</commentid>
    <comment_count>7</comment_count>
    <who name="">bjoernv</who>
    <bug_when>2007-01-17 19:23:23 +0000</bug_when>
    <thetext>Yes, good. In cases, where the group could not be preserved, special lower permissions from other/world could be used. For user root, the file owner could also be preserved. I saw such code in Gedit and VIM.  </thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>502182</commentid>
    <comment_count>8</comment_count>
    <who name="David Faure">faure</who>
    <bug_when>2007-01-17 19:28:45 +0000</bug_when>
    <thetext>On Wednesday 17 January 2007 19:23, bjoern@cs.tu-berlin.de wrote:
&gt; For user root, the file owner could also be preserved.

Well, KDE is not supposed to be run as root...
</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>502184</commentid>
    <comment_count>9</comment_count>
    <who name="">bjoernv</who>
    <bug_when>2007-01-17 19:39:40 +0000</bug_when>
    <thetext>Yes, I agree that running KDE as root is generally no good idea. But running a KDE editor (kate, kwrite) as root? Some users may use the KDE editors for editing system files (e.g. &quot;kdesu kate /etc/hosts&quot;).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>502254</commentid>
    <comment_count>10</comment_count>
    <who name="David Faure">faure</who>
    <bug_when>2007-01-18 01:19:08 +0000</bug_when>
    <thetext>Hmm, right. So this is indeed about chown+chgrp.
</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>541778</commentid>
    <comment_count>11</comment_count>
    <who name="David Faure">faure</who>
    <bug_when>2007-08-20 13:56:57 +0000</bug_when>
    <thetext>Martin Koller added KIO::chown()  [which takes user and group] in kdelibs4.

However I think the actual fix for file_copy is to add a chown+chgrp call at the end of FileProtocol::copy(), much like we preserve modification time there,
and a call in CopyJob for the case of copying directories (but that&apos;s more for konqueror than for kate).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>554188</commentid>
    <comment_count>12</comment_count>
    <who name="Christoph Cullmann">christoph</who>
    <bug_when>2007-11-08 19:12:42 +0000</bug_when>
    <thetext>In which way the kate code needs fixes for KDE 4 now? Would be happy about some hints.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>554277</commentid>
    <comment_count>13</comment_count>
    <who name="David Faure">faure</who>
    <bug_when>2007-11-09 12:28:19 +0000</bug_when>
    <thetext>Kate doesn&apos;t need fixing for this, as long as it uses KIO::file_copy. The fix has to be made in kio_file.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1146085</commentid>
    <comment_count>14</comment_count>
    <who name="Christoph Feck">cfeck</who>
    <bug_when>2011-07-27 13:09:57 +0000</bug_when>
    <thetext>David, what is the status of this bug?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1656091</commentid>
    <comment_count>15</comment_count>
    <who name="Martin Koller">martin</who>
    <bug_when>2017-01-22 19:50:47 +0000</bug_when>
    <thetext>Git commit d252f7958633f3374e77e1cc52f11c534ca55219 by Martin Koller.
Committed on 22/01/2017 at 19:49.
Pushed by mkoller into branch &apos;master&apos;.

preserve group/owner on file copy
REVIEW: 129864

M  +28   -12   src/ioslaves/file/file_unix.cpp

https://commits.kde.org/kio/d252f7958633f3374e77e1cc52f11c534ca55219</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1669863</commentid>
    <comment_count>16</comment_count>
    <who name="Maximiliano Curia">maxy</who>
    <bug_when>2017-04-04 10:02:03 +0000</bug_when>
    <thetext>(In reply to Martin Koller from comment #15)
&gt; Git commit d252f7958633f3374e77e1cc52f11c534ca55219 by Martin Koller.
&gt; Committed on 22/01/2017 at 19:49.
&gt; Pushed by mkoller into branch &apos;master&apos;.
&gt; 
&gt; preserve group/owner on file copy
&gt; REVIEW: 129864
&gt; 
&gt; M  +28   -12   src/ioslaves/file/file_unix.cpp
&gt; 
&gt; https://commits.kde.org/kio/d252f7958633f3374e77e1cc52f11c534ca55219

This patch breaks the setgid directories and default acl semantics. It might be better to avoid setting the group if the dest dir is setgid, and avoid setting the file permissions if the destdir has a default acl value.

Happy hacking,</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1704815</commentid>
    <comment_count>17</comment_count>
    <who name="David Faure">faure</who>
    <bug_when>2017-10-07 17:38:56 +0000</bug_when>
    <thetext>Martin Koller, can you take a look at the regression due to your patch, as per the last comment?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1704827</commentid>
    <comment_count>18</comment_count>
    <who name="">bjoernv</who>
    <bug_when>2017-10-07 22:27:07 +0000</bug_when>
    <thetext>(In reply to Maximiliano Curia from comment #16)
&gt; This patch breaks the setgid directories and default acl semantics. It might
&gt; be better to avoid setting the group if the dest dir is setgid, and avoid
&gt; setting the file permissions if the destdir has a default acl value.

I agree in case of normal copy operations. But in case of Kate editor&apos;s backups (initial subject of this report) it may not be wise to ignore to set permissions or groups in special situations. Both can again result in situations, where some users have more permissions on the backup file than on the original.

A flag like &quot;--preserve&quot; for the &quot;cp&quot; command can be used to make preserving of permissions and groups optional.

I can recommend the file operations code of the VIM editor. It contains code for handling backups and all kinds of ACLs:

https://github.com/vim/vim/blob/master/src/fileio.c
https://github.com/vim/vim/blob/master/src/os_unix.c</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1708132</commentid>
    <comment_count>19</comment_count>
    <who name="Christoph Feck">cfeck</who>
    <bug_when>2017-10-25 18:02:29 +0000</bug_when>
    <thetext>Martin, any update?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1708912</commentid>
    <comment_count>20</comment_count>
    <who name="Martin Koller">martin</who>
    <bug_when>2017-10-30 06:02:07 +0000</bug_when>
    <thetext>It seems there is disagreement between people to how this actually should work.
Since I have no experience with ACL, I did not look into this further.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2055130</commentid>
    <comment_count>21</comment_count>
    <who name="David Faure">faure</who>
    <bug_when>2021-08-22 08:28:56 +0000</bug_when>
    <thetext>Git commit ff4bb61b2f5492f1942c1f11fe4c8b9a7eb654ea by David Faure, on behalf of Ahmad Samir.
Committed on 22/08/2021 at 08:28.
Pushed by dfaure into branch &apos;master&apos;.

file_unix: minor cleanup

Separate ::chmod() from acl_set_file() call, more readable that way.

If the permissions are -1, that means preserve the permissions/attributes
of the source file, this matches what the code is/was doing and the docs.
Maybe we should add a couple more setters for the job, e.g. setPermissions(),
setAcl(), then users can fine-tune the permissions of the dest, preserve,
don&apos;t set, leave as default

M  +9    -5    src/ioslaves/file/file_unix.cpp

https://invent.kde.org/frameworks/kio/commit/ff4bb61b2f5492f1942c1f11fe4c8b9a7eb654ea</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2062121</commentid>
    <comment_count>22</comment_count>
    <who name="Ahmad Samir">a.samirh78</who>
    <bug_when>2021-09-19 10:09:43 +0000</bug_when>
    <thetext>Git commit de7aff60e81b05239fd3d6c17895c48a985dd27a by Ahmad Samir.
Committed on 19/09/2021 at 10:06.
Pushed by ahmadsamir into branch &apos;master&apos;.

Fix permissions when copying files

This reverts commit 7863f595991c5, I was wrong :)

Reading the code some more (in copyjob, file_copy and file_unix), the
permissions arg is &apos;-1&apos; when we shouldn&apos;t touch the dest permissions at all,
i.e. leave them as the system default permissions for newly created files
(umask ...etc).

The scenarios are:
- calling KIO::copy() A to B, permissions are preserved by default, i.e. the
  job copies A&apos;s permissions to the permissions arg when it calls file_copy
- calling KIO::copy() A to B, and calling setDefaultPermissions(true),
  the job sets the permissions arg to &apos;-1&apos; when it calls file_copy
- calling KIO::file_copy() directly, the you have full control, if you want
  to preserve the permissions use A&apos;s permissions as the permissions arg,
  otherwise use &apos;-1&apos; to use the system default permissions

ACL attributes are handled implicitly at the moment, which isn&apos;t ideal but
can be improved later on.
Related: bug 404777

FIXED-IN: 5.87

M  +4    -6    src/core/filecopyjob.h
M  +15   -22   src/ioslaves/file/file_unix.cpp

https://invent.kde.org/frameworks/kio/commit/de7aff60e81b05239fd3d6c17895c48a985dd27a</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2062468</commentid>
    <comment_count>23</comment_count>
    <who name="Nate Graham">nate</who>
    <bug_when>2021-09-20 16:29:04 +0000</bug_when>
    <thetext>Anything more to do here, or is this fully fixed now?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2491979</commentid>
    <comment_count>24</comment_count>
    <who name="TraceyC">kdedev</who>
    <bug_when>2026-02-06 17:58:52 +0000</bug_when>
    <thetext>Closing, due to the age of this issue. If there is still work to be done, please open a new report. Thanks!</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>