When does chmod fail?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
8
down vote

favorite
3












Under what circumstances will chmod fail?



I looked at the man page but it only specifies usage and doesn't go into details about what circumstances it won't work in.



I'd assume chmod will work if:



  • you're root

  • you own the target file (and are setting a mundane mode bit i.e. not sticky bit, others)

Can users use chmod to change permissions on a file they have group access for? Is it related to read/write access?










share|improve this question



















  • 2




    It will fail if the inode can't be modified, eg the filesystem is read only.
    – jordanm
    Oct 22 '12 at 4:00










  • Related: Can I allow users to chmod a file not owned by them?
    – Palec
    Feb 22 '15 at 22:59














up vote
8
down vote

favorite
3












Under what circumstances will chmod fail?



I looked at the man page but it only specifies usage and doesn't go into details about what circumstances it won't work in.



I'd assume chmod will work if:



  • you're root

  • you own the target file (and are setting a mundane mode bit i.e. not sticky bit, others)

Can users use chmod to change permissions on a file they have group access for? Is it related to read/write access?










share|improve this question



















  • 2




    It will fail if the inode can't be modified, eg the filesystem is read only.
    – jordanm
    Oct 22 '12 at 4:00










  • Related: Can I allow users to chmod a file not owned by them?
    – Palec
    Feb 22 '15 at 22:59












up vote
8
down vote

favorite
3









up vote
8
down vote

favorite
3






3





Under what circumstances will chmod fail?



I looked at the man page but it only specifies usage and doesn't go into details about what circumstances it won't work in.



I'd assume chmod will work if:



  • you're root

  • you own the target file (and are setting a mundane mode bit i.e. not sticky bit, others)

Can users use chmod to change permissions on a file they have group access for? Is it related to read/write access?










share|improve this question















Under what circumstances will chmod fail?



I looked at the man page but it only specifies usage and doesn't go into details about what circumstances it won't work in.



I'd assume chmod will work if:



  • you're root

  • you own the target file (and are setting a mundane mode bit i.e. not sticky bit, others)

Can users use chmod to change permissions on a file they have group access for? Is it related to read/write access?







ubuntu permissions chmod access-control






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 3 '16 at 21:28









clk

1,5541821




1,5541821










asked Oct 22 '12 at 1:26









Wug

148116




148116







  • 2




    It will fail if the inode can't be modified, eg the filesystem is read only.
    – jordanm
    Oct 22 '12 at 4:00










  • Related: Can I allow users to chmod a file not owned by them?
    – Palec
    Feb 22 '15 at 22:59












  • 2




    It will fail if the inode can't be modified, eg the filesystem is read only.
    – jordanm
    Oct 22 '12 at 4:00










  • Related: Can I allow users to chmod a file not owned by them?
    – Palec
    Feb 22 '15 at 22:59







2




2




It will fail if the inode can't be modified, eg the filesystem is read only.
– jordanm
Oct 22 '12 at 4:00




It will fail if the inode can't be modified, eg the filesystem is read only.
– jordanm
Oct 22 '12 at 4:00












Related: Can I allow users to chmod a file not owned by them?
– Palec
Feb 22 '15 at 22:59




Related: Can I allow users to chmod a file not owned by them?
– Palec
Feb 22 '15 at 22:59










4 Answers
4






active

oldest

votes

















up vote
3
down vote













Only the owner of the file, or the root user, may change a file's permissions. The current permissions on the file or on its parent directory are irrelevant¹. This is specified in POSIX:




The application shall ensure that the effective user ID of the process matches the owner of the file or the process has appropriate privileges in order to do this.




On most unices, “appropriate privileges” means running as root. If these conditions are not met, chmod usually fails with EPERM, though other behaviors such as aborting the program due to a security violation are permitted.



In addition, some unix variants have system-specific ways of authorizing or forbidding chmod. For example, Linux has a capability (CAP_FOWNER) that allows processes to change a file's permissions and other metadata regardless of its owner.



There are other reasons chmod might fail even though the file exists, is accessible and has the appropriate owner. Common ones include a read-only filesystem or a filesystem that does not support permissions such as FAT. Less common ones include system-specific restrictions such as the immutable attribute on Linux's ext2 filesystem and successors.



¹ Except insofar as he process running chmod must be able to access the file, so it must have execute permission on the directory containing the file and any other directory that it traverses to do so.






share|improve this answer



























    up vote
    2
    down vote













    The details you want are in the manual page for the chmod() system call. Instead of man chmod use man 2 chmod. man chattr and man 2 setxattr will interest you as well; the file attributes that chattr/setxattr() set augment the behavior of the traditional Unix permissions set by chmod.






    share|improve this answer




















    • I'll try this when I get out of work.
      – Wug
      Oct 22 '12 at 12:58

















    up vote
    1
    down vote













    According to the UNIX standard, "The effective user ID of the process must match the owner of the file or the process must have appropriate privileges in order to do this."



    The bit about appropriate privileges needs some explanation. On traditional systems, chmod is allowed on all files when the effective UID (on Linux the filesystem UID, but see below) of the process is 0 [i.e. root].



    Linux has a system called capabilities, and the CAP_FOWNER bit controls the ability to use chmod on all files. By default, all capabilities are granted when an execve() call creates a root process (either by executing a setuid binary or when the real UID is 0) or when the effective UID is set to 0 (and removed when it is set to a nonzero value), and a set of capabilities including CAP_FOWNER are enabled when the filesystem UID is set to 0 (and disabled when it is set to a nonzero value). Read the manpage for more details.



    You mentioned the sticky bit, but omitted the fact that users also may not set the setgid bit on a file when they are not in the group that is assigned to the file. The setuid or setgid bit may also be ignored in additional implementation-defined circumstances.






    share|improve this answer





























      up vote
      0
      down vote














      Can users use chmod to change permissions on a file they have group access for?




      Why don't you just try and see?



      $ touch foo
      $ sudo install -o root -g $(id -gn) -m660 foo bar
      $ ls -la bar
      -rw-rw---- 1 root staff 0 Oct 21 21:33 bar
      $ chmod g-w bar
      chmod: bar: Operation not permitted
      $ chmod g+x bar
      chmod: bar: Operation not permitted





      share|improve this answer




















      • I have been poking at it, but this is security related and I don't want to accidentally miss an edge case.
        – Wug
        Oct 22 '12 at 2:43











      • If you're worried about an edge case, you can (at least to minimize risks) do the chmod, and then check the permissions on it. If you're feeling paranoid, do an fsync before checking.
        – hexafraction
        Oct 22 '12 at 10:32










      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f52519%2fwhen-does-chmod-fail%23new-answer', 'question_page');

      );

      Post as a guest






























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote













      Only the owner of the file, or the root user, may change a file's permissions. The current permissions on the file or on its parent directory are irrelevant¹. This is specified in POSIX:




      The application shall ensure that the effective user ID of the process matches the owner of the file or the process has appropriate privileges in order to do this.




      On most unices, “appropriate privileges” means running as root. If these conditions are not met, chmod usually fails with EPERM, though other behaviors such as aborting the program due to a security violation are permitted.



      In addition, some unix variants have system-specific ways of authorizing or forbidding chmod. For example, Linux has a capability (CAP_FOWNER) that allows processes to change a file's permissions and other metadata regardless of its owner.



      There are other reasons chmod might fail even though the file exists, is accessible and has the appropriate owner. Common ones include a read-only filesystem or a filesystem that does not support permissions such as FAT. Less common ones include system-specific restrictions such as the immutable attribute on Linux's ext2 filesystem and successors.



      ¹ Except insofar as he process running chmod must be able to access the file, so it must have execute permission on the directory containing the file and any other directory that it traverses to do so.






      share|improve this answer
























        up vote
        3
        down vote













        Only the owner of the file, or the root user, may change a file's permissions. The current permissions on the file or on its parent directory are irrelevant¹. This is specified in POSIX:




        The application shall ensure that the effective user ID of the process matches the owner of the file or the process has appropriate privileges in order to do this.




        On most unices, “appropriate privileges” means running as root. If these conditions are not met, chmod usually fails with EPERM, though other behaviors such as aborting the program due to a security violation are permitted.



        In addition, some unix variants have system-specific ways of authorizing or forbidding chmod. For example, Linux has a capability (CAP_FOWNER) that allows processes to change a file's permissions and other metadata regardless of its owner.



        There are other reasons chmod might fail even though the file exists, is accessible and has the appropriate owner. Common ones include a read-only filesystem or a filesystem that does not support permissions such as FAT. Less common ones include system-specific restrictions such as the immutable attribute on Linux's ext2 filesystem and successors.



        ¹ Except insofar as he process running chmod must be able to access the file, so it must have execute permission on the directory containing the file and any other directory that it traverses to do so.






        share|improve this answer






















          up vote
          3
          down vote










          up vote
          3
          down vote









          Only the owner of the file, or the root user, may change a file's permissions. The current permissions on the file or on its parent directory are irrelevant¹. This is specified in POSIX:




          The application shall ensure that the effective user ID of the process matches the owner of the file or the process has appropriate privileges in order to do this.




          On most unices, “appropriate privileges” means running as root. If these conditions are not met, chmod usually fails with EPERM, though other behaviors such as aborting the program due to a security violation are permitted.



          In addition, some unix variants have system-specific ways of authorizing or forbidding chmod. For example, Linux has a capability (CAP_FOWNER) that allows processes to change a file's permissions and other metadata regardless of its owner.



          There are other reasons chmod might fail even though the file exists, is accessible and has the appropriate owner. Common ones include a read-only filesystem or a filesystem that does not support permissions such as FAT. Less common ones include system-specific restrictions such as the immutable attribute on Linux's ext2 filesystem and successors.



          ¹ Except insofar as he process running chmod must be able to access the file, so it must have execute permission on the directory containing the file and any other directory that it traverses to do so.






          share|improve this answer












          Only the owner of the file, or the root user, may change a file's permissions. The current permissions on the file or on its parent directory are irrelevant¹. This is specified in POSIX:




          The application shall ensure that the effective user ID of the process matches the owner of the file or the process has appropriate privileges in order to do this.




          On most unices, “appropriate privileges” means running as root. If these conditions are not met, chmod usually fails with EPERM, though other behaviors such as aborting the program due to a security violation are permitted.



          In addition, some unix variants have system-specific ways of authorizing or forbidding chmod. For example, Linux has a capability (CAP_FOWNER) that allows processes to change a file's permissions and other metadata regardless of its owner.



          There are other reasons chmod might fail even though the file exists, is accessible and has the appropriate owner. Common ones include a read-only filesystem or a filesystem that does not support permissions such as FAT. Less common ones include system-specific restrictions such as the immutable attribute on Linux's ext2 filesystem and successors.



          ¹ Except insofar as he process running chmod must be able to access the file, so it must have execute permission on the directory containing the file and any other directory that it traverses to do so.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 23 '12 at 0:07









          Gilles

          513k12010161548




          513k12010161548






















              up vote
              2
              down vote













              The details you want are in the manual page for the chmod() system call. Instead of man chmod use man 2 chmod. man chattr and man 2 setxattr will interest you as well; the file attributes that chattr/setxattr() set augment the behavior of the traditional Unix permissions set by chmod.






              share|improve this answer




















              • I'll try this when I get out of work.
                – Wug
                Oct 22 '12 at 12:58














              up vote
              2
              down vote













              The details you want are in the manual page for the chmod() system call. Instead of man chmod use man 2 chmod. man chattr and man 2 setxattr will interest you as well; the file attributes that chattr/setxattr() set augment the behavior of the traditional Unix permissions set by chmod.






              share|improve this answer




















              • I'll try this when I get out of work.
                – Wug
                Oct 22 '12 at 12:58












              up vote
              2
              down vote










              up vote
              2
              down vote









              The details you want are in the manual page for the chmod() system call. Instead of man chmod use man 2 chmod. man chattr and man 2 setxattr will interest you as well; the file attributes that chattr/setxattr() set augment the behavior of the traditional Unix permissions set by chmod.






              share|improve this answer












              The details you want are in the manual page for the chmod() system call. Instead of man chmod use man 2 chmod. man chattr and man 2 setxattr will interest you as well; the file attributes that chattr/setxattr() set augment the behavior of the traditional Unix permissions set by chmod.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Oct 22 '12 at 4:37









              Kyle Jones

              11.3k13048




              11.3k13048











              • I'll try this when I get out of work.
                – Wug
                Oct 22 '12 at 12:58
















              • I'll try this when I get out of work.
                – Wug
                Oct 22 '12 at 12:58















              I'll try this when I get out of work.
              – Wug
              Oct 22 '12 at 12:58




              I'll try this when I get out of work.
              – Wug
              Oct 22 '12 at 12:58










              up vote
              1
              down vote













              According to the UNIX standard, "The effective user ID of the process must match the owner of the file or the process must have appropriate privileges in order to do this."



              The bit about appropriate privileges needs some explanation. On traditional systems, chmod is allowed on all files when the effective UID (on Linux the filesystem UID, but see below) of the process is 0 [i.e. root].



              Linux has a system called capabilities, and the CAP_FOWNER bit controls the ability to use chmod on all files. By default, all capabilities are granted when an execve() call creates a root process (either by executing a setuid binary or when the real UID is 0) or when the effective UID is set to 0 (and removed when it is set to a nonzero value), and a set of capabilities including CAP_FOWNER are enabled when the filesystem UID is set to 0 (and disabled when it is set to a nonzero value). Read the manpage for more details.



              You mentioned the sticky bit, but omitted the fact that users also may not set the setgid bit on a file when they are not in the group that is assigned to the file. The setuid or setgid bit may also be ignored in additional implementation-defined circumstances.






              share|improve this answer


























                up vote
                1
                down vote













                According to the UNIX standard, "The effective user ID of the process must match the owner of the file or the process must have appropriate privileges in order to do this."



                The bit about appropriate privileges needs some explanation. On traditional systems, chmod is allowed on all files when the effective UID (on Linux the filesystem UID, but see below) of the process is 0 [i.e. root].



                Linux has a system called capabilities, and the CAP_FOWNER bit controls the ability to use chmod on all files. By default, all capabilities are granted when an execve() call creates a root process (either by executing a setuid binary or when the real UID is 0) or when the effective UID is set to 0 (and removed when it is set to a nonzero value), and a set of capabilities including CAP_FOWNER are enabled when the filesystem UID is set to 0 (and disabled when it is set to a nonzero value). Read the manpage for more details.



                You mentioned the sticky bit, but omitted the fact that users also may not set the setgid bit on a file when they are not in the group that is assigned to the file. The setuid or setgid bit may also be ignored in additional implementation-defined circumstances.






                share|improve this answer
























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  According to the UNIX standard, "The effective user ID of the process must match the owner of the file or the process must have appropriate privileges in order to do this."



                  The bit about appropriate privileges needs some explanation. On traditional systems, chmod is allowed on all files when the effective UID (on Linux the filesystem UID, but see below) of the process is 0 [i.e. root].



                  Linux has a system called capabilities, and the CAP_FOWNER bit controls the ability to use chmod on all files. By default, all capabilities are granted when an execve() call creates a root process (either by executing a setuid binary or when the real UID is 0) or when the effective UID is set to 0 (and removed when it is set to a nonzero value), and a set of capabilities including CAP_FOWNER are enabled when the filesystem UID is set to 0 (and disabled when it is set to a nonzero value). Read the manpage for more details.



                  You mentioned the sticky bit, but omitted the fact that users also may not set the setgid bit on a file when they are not in the group that is assigned to the file. The setuid or setgid bit may also be ignored in additional implementation-defined circumstances.






                  share|improve this answer














                  According to the UNIX standard, "The effective user ID of the process must match the owner of the file or the process must have appropriate privileges in order to do this."



                  The bit about appropriate privileges needs some explanation. On traditional systems, chmod is allowed on all files when the effective UID (on Linux the filesystem UID, but see below) of the process is 0 [i.e. root].



                  Linux has a system called capabilities, and the CAP_FOWNER bit controls the ability to use chmod on all files. By default, all capabilities are granted when an execve() call creates a root process (either by executing a setuid binary or when the real UID is 0) or when the effective UID is set to 0 (and removed when it is set to a nonzero value), and a set of capabilities including CAP_FOWNER are enabled when the filesystem UID is set to 0 (and disabled when it is set to a nonzero value). Read the manpage for more details.



                  You mentioned the sticky bit, but omitted the fact that users also may not set the setgid bit on a file when they are not in the group that is assigned to the file. The setuid or setgid bit may also be ignored in additional implementation-defined circumstances.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Oct 22 '12 at 17:22

























                  answered Oct 22 '12 at 17:15









                  Random832

                  8,36012235




                  8,36012235




















                      up vote
                      0
                      down vote














                      Can users use chmod to change permissions on a file they have group access for?




                      Why don't you just try and see?



                      $ touch foo
                      $ sudo install -o root -g $(id -gn) -m660 foo bar
                      $ ls -la bar
                      -rw-rw---- 1 root staff 0 Oct 21 21:33 bar
                      $ chmod g-w bar
                      chmod: bar: Operation not permitted
                      $ chmod g+x bar
                      chmod: bar: Operation not permitted





                      share|improve this answer




















                      • I have been poking at it, but this is security related and I don't want to accidentally miss an edge case.
                        – Wug
                        Oct 22 '12 at 2:43











                      • If you're worried about an edge case, you can (at least to minimize risks) do the chmod, and then check the permissions on it. If you're feeling paranoid, do an fsync before checking.
                        – hexafraction
                        Oct 22 '12 at 10:32














                      up vote
                      0
                      down vote














                      Can users use chmod to change permissions on a file they have group access for?




                      Why don't you just try and see?



                      $ touch foo
                      $ sudo install -o root -g $(id -gn) -m660 foo bar
                      $ ls -la bar
                      -rw-rw---- 1 root staff 0 Oct 21 21:33 bar
                      $ chmod g-w bar
                      chmod: bar: Operation not permitted
                      $ chmod g+x bar
                      chmod: bar: Operation not permitted





                      share|improve this answer




















                      • I have been poking at it, but this is security related and I don't want to accidentally miss an edge case.
                        – Wug
                        Oct 22 '12 at 2:43











                      • If you're worried about an edge case, you can (at least to minimize risks) do the chmod, and then check the permissions on it. If you're feeling paranoid, do an fsync before checking.
                        – hexafraction
                        Oct 22 '12 at 10:32












                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote










                      Can users use chmod to change permissions on a file they have group access for?




                      Why don't you just try and see?



                      $ touch foo
                      $ sudo install -o root -g $(id -gn) -m660 foo bar
                      $ ls -la bar
                      -rw-rw---- 1 root staff 0 Oct 21 21:33 bar
                      $ chmod g-w bar
                      chmod: bar: Operation not permitted
                      $ chmod g+x bar
                      chmod: bar: Operation not permitted





                      share|improve this answer













                      Can users use chmod to change permissions on a file they have group access for?




                      Why don't you just try and see?



                      $ touch foo
                      $ sudo install -o root -g $(id -gn) -m660 foo bar
                      $ ls -la bar
                      -rw-rw---- 1 root staff 0 Oct 21 21:33 bar
                      $ chmod g-w bar
                      chmod: bar: Operation not permitted
                      $ chmod g+x bar
                      chmod: bar: Operation not permitted






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Oct 22 '12 at 1:36









                      dubiousjim

                      1,9581223




                      1,9581223











                      • I have been poking at it, but this is security related and I don't want to accidentally miss an edge case.
                        – Wug
                        Oct 22 '12 at 2:43











                      • If you're worried about an edge case, you can (at least to minimize risks) do the chmod, and then check the permissions on it. If you're feeling paranoid, do an fsync before checking.
                        – hexafraction
                        Oct 22 '12 at 10:32
















                      • I have been poking at it, but this is security related and I don't want to accidentally miss an edge case.
                        – Wug
                        Oct 22 '12 at 2:43











                      • If you're worried about an edge case, you can (at least to minimize risks) do the chmod, and then check the permissions on it. If you're feeling paranoid, do an fsync before checking.
                        – hexafraction
                        Oct 22 '12 at 10:32















                      I have been poking at it, but this is security related and I don't want to accidentally miss an edge case.
                      – Wug
                      Oct 22 '12 at 2:43





                      I have been poking at it, but this is security related and I don't want to accidentally miss an edge case.
                      – Wug
                      Oct 22 '12 at 2:43













                      If you're worried about an edge case, you can (at least to minimize risks) do the chmod, and then check the permissions on it. If you're feeling paranoid, do an fsync before checking.
                      – hexafraction
                      Oct 22 '12 at 10:32




                      If you're worried about an edge case, you can (at least to minimize risks) do the chmod, and then check the permissions on it. If you're feeling paranoid, do an fsync before checking.
                      – hexafraction
                      Oct 22 '12 at 10:32

















                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f52519%2fwhen-does-chmod-fail%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay