dos2unix fails to change the owner and group of temporary output file in RHEL 7

Solution Verified - Updated -

Environment

  • Red Hat Enterprise Linux (RHEL) 7
  • dos2unix-6.0.3

Issue

  1. User A creates a file with rw permissions for all users:
$ touch test_file
$ chmod a+rw test_file
  1. User B runs dos2unix on the file:
$ dos2unix test_file
dos2unix: Failed to change the owner and group of temporary output file ./d2utmpLCmQXP: Operation not permitted
dos2unix: converting file test_file to Unix format ...
dos2unix: problems converting file test_file

There were no issues in the same scenario on RHEL 6 with dos2unix-3.1.

Resolution

If preserving ownership and group of converted file is not essential in your use case please use dos2unix in New file mode instead of the default Old file mode, i.e.:

$ dos2unix -n test_file tmp_file
$ mv -f tmp_file test_file

Root Cause

The feature of preserving the original permissions was introduced in version 5.3.1 due to security reasons. The security risk of old behaviour is described in dos2unix man page:

       -o, --oldfile FILE ...
           Old file mode. Convert file FILE and overwrite output to it. The
           program defaults to run in this mode. Wildcard names may be used.

           In old file (in-place) mode the converted file gets the same owner,
           group, and read/write permissions as the original file. Also when
           the file is converted by another user who has write permissions on
           the file (e.g. user root).  The conversion will be aborted when it
           is not possible to preserve the original values.  Change of owner
           could mean that the original owner is not able to read the file any
           more. Change of group could be a security risk, the file could be
           made readable for persons for whom it is not intended.
           Preservation of owner, group, and read/write permissions is only
           supported on Unix.

Enhancement proposal to add option allowing conversion without preserving the original permissions is tracked by Bug 1483633.

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments