Bug 1200381 (CVE-2022-48682) - VUL-0: CVE-2022-48682: fdupes: TOCTOU race allows arbitrary file deletion
Summary: VUL-0: CVE-2022-48682: fdupes: TOCTOU race allows arbitrary file deletion
Status: RESOLVED FIXED
Alias: CVE-2022-48682
Product: SUSE Security Incidents
Classification: Novell Products
Component: Incidents (show other bugs)
Version: unspecified
Hardware: Other Other
: P3 - Medium : Normal
Target Milestone: ---
Assignee: Security Team bot
QA Contact: Security Team bot
URL: https://smash.suse.de/issue/334079/
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-06-09 13:38 UTC by Wolfgang Frisch
Modified: 2024-06-20 14:01 UTC (History)
1 user (show)

See Also:
Found By: ---
Services Priority:
Business Priority:
Blocker: ---
Marketing QA Status: ---
IT Deployment: ---


Attachments
poc-fdupes-file-deletion.c (7.83 KB, text/plain)
2022-06-09 18:25 UTC, Wolfgang Frisch
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Wolfgang Frisch 2022-06-09 13:38:09 UTC
fdupes [1] can be tricked into deleting arbitrary system files by exploiting a TOCTOU race condition in `fdupes.c` when it's executed as root, operating on a folder controlled by an unprivileged user.

[1] https://github.com/adrianlopezroche/fdupes
Comment 1 Wolfgang Frisch 2022-06-09 18:25:32 UTC
Created attachment 859510 [details]
poc-fdupes-file-deletion.c

Reproducer

gcc -O2 -o poc-fdupes-file-deletion poc-fdupes-file-deletion.c

usage: poc-fdupes-file-deletion USERDIR TARGETFILE

USERDIR must be writable for the current user.
TARGETFILE must be readable for the current user.

The target file will be deleted when root executes `fdupes -r -d -N USERDIR`.
Comment 2 Wolfgang Frisch 2022-06-09 20:34:48 UTC
fdupes.c [1] contains a file system race condition between grokdir() and
deletefiles() that can be exploited to make it follow symlinks, and
under special circumstances to delete arbitrary files outside the
scanned directory.

When fdupes is instructed to delete duplicates, it first scans the
destination directory recursively, while ignoring symlinks. Later on,
deletefiles() operates on the metadata gathered by grokdir().
Critically, deletefiles() assumes the file system contents has not been
altered in the meantime. A malicious user can win the race by monitoring
the directory with inotify(7) and replace a subdirectory with a symlink
at the right time. The time to win this race is very short and would
often fail. This issue is solved by creating a "tar pit" directory tree
that keeps fdupes busy while we place the symlink. Approx. 1000
subdirectories were found to be sufficient in this case.

In the special case of a privileged user running `fdupes -r -d -N` on a
directory with subdirectories writable to an unprivileged malicious
user, it is possible to delete arbitrary files on the system.

Scenario:

- Privileged user executes: fdupes -r -d -N $USERDIR

- Malicious user, with write a access to a subdirectory in USERDIR,
  wants to delete TARGETFILE, which they have read access to, e.g.
  `/etc/passwd`.

Process:

Set up a directory structure with 2 copies of TARGETFILE, one of which
must have an mtime older than the original. We also need the
aforementioned "tar pit":

```
userdir/
└── workdir.123456789/
    ├── redherring/
    │   └── TARGETNAME (copy of TARGETFILE with mtime < original)
    └── bait/
        ├── TARGETNAME (copy of TARGETFILE)
        └── tarpit
            ├── 0
            ├── 1
            ├── …
            └── 999
```

Now wait until bait/ is accessed and replace it with a symlink to the
directory containing TARGETFILE.

fdupes will delete TARGETFILE.

[1] https://github.com/adrianlopezroche/fdupes/blob/4b6bcde1b3eb1cebe87cd30814f7d6cf4ee46e95/fdupes.c
Comment 4 Wolfgang Frisch 2022-07-07 19:13:06 UTC
Still waiting for upstream to respond.
Comment 5 Wolfgang Frisch 2022-09-20 13:33:04 UTC
The issue has been confirmed by upstream and a solution is underway.
Comment 7 Wolfgang Frisch 2024-04-15 08:38:25 UTC
6.0 CVSS:3.1/AV:L/AC:H/PR:L/UI:R/S:U/C:N/I:H/A:H
Comment 8 Wolfgang Frisch 2024-04-15 08:42:02 UTC
Fixed in upstream release 2.2.0

openSUSE:Factory        Already fixed
SUSE:SLE-12:Update      Affected
SUSE:SLE-15-SP4:Update  Affected

SLE-15-SP4 (fdupes-2.1.2) can be bumped to the latest version 2.3.0.
There are no breaking changes, only bug fixes:
https://github.com/adrianlopezroche/fdupes/blob/master/CHANGES
Comment 14 Wolfgang Frisch 2024-04-29 12:28:08 UTC
CVE-2022-48682 was assigned to this bug.
Comment 15 Maintenance Automation 2024-05-31 08:30:10 UTC
SUSE-RU-2024:1878-1: An update that has one fix can now be installed.

Category: recommended (moderate)
Bug References: 1200381
Maintenance Incident: [SUSE:Maintenance:33850](https://smelt.suse.de/incident/33850/)
Sources used:
SUSE Linux Enterprise High Performance Computing 12 SP5 (src):
 fdupes-1.61-8.3.1
SUSE Linux Enterprise Server 12 SP5 (src):
 fdupes-1.61-8.3.1
SUSE Linux Enterprise Server for SAP Applications 12 SP5 (src):
 fdupes-1.61-8.3.1

NOTE: This line indicates an update has been released for the listed product(s). At times this might be only a partial fix. If you have questions please reach out to maintenance coordination.
Comment 16 Wolfgang Frisch 2024-06-20 14:01:10 UTC
Perfect. Thanks for the submissions!
Resolved!