Skip to content
masdzub.com
~/articles/debian — one from the notebook

Auditing SUID and SGID Files on Debian 12 with find and dpkg

Cross-reference find's filesystem scan against dpkg's package manifest to find SUID and SGID binaries no installed package is willing to claim.

masdzub@website:~$ cat ~/articles/auditing-suid-and-sgid-files-on-debian-12-with-find-and-dpkg.aspx.md
# 1 file · 15 min read · topic: debian · status: published

Introduction

On Debian 12, a useful first inventory pairs a filesystem scan with package metadata. find identifies files whose SUID or SGID bits are set; dpkg-query -S can identify package-owned paths. A file that has no matching package record is an investigation lead, not proof of compromise: administrators, deployment systems, alternatives, or package maintainer scripts may also create files. Conversely, package ownership alone does not prove that a particular permission bit is expected or safe.

The tools used here are available from Debian’s standard packages. The workflow is read-only until you choose to remediate a finding. Establish a trusted baseline, review exceptions with the system owner, and use other evidence such as package metadata, hashes, deployment records, and mount options before changing permissions.

What SUID and SGID actually do on Debian 12

A SUID executable runs with the effective user ID of its owner, not the caller. Since the owner is almost always root, the binary grants any logged-in user exactly the operations the program was written to allow. passwd is the textbook case: any user runs it, the process briefly holds root, it writes to /etc/shadow through a tightly controlled code path, then drops back. The whole model depends on the binary being correct. A buffer overflow in a SUID root program is a local privilege escalation, full stop.

SGID applies the same idea to the group ID. On executables, an SGID binary runs with the effective group of the file’s group, which is how wall has historically written to every terminal without needing root, and how chage and expiry touch the shadow group. On directories, SGID means something different and usually benign: new files created inside inherit the directory’s group. That is how shared team directories work. Because the two uses are unrelated, decide up front whether your audit is about files only, or about files plus directories. Most operators audit files, then handle SGID directories separately.

The attack surface is the union of all SUID and SGID executables. Each one is a piece of code any user can invoke with privileges the user does not have. Auditing them reduces to three questions per file: does Debian actually ship this, does the owning package expect the bit to be set, and has anything changed since the package installed it.

Why a package-based audit beats trusting a filesystem scanner

A filesystem integrity checker and the package database answer different questions. A hash change may be a package upgrade, an administrator edit, or an unexpected modification. dpkg-query -S can show package ownership for paths recorded in dpkg’s database, but it does not list extra files created by maintainer scripts or alternatives. It also does not prove that a package expects the current SUID/SGID mode. Use the scan and package query as triage, then compare package contents, package verification data, local deployment records, and the trusted baseline.

A path that dpkg-query -S cannot resolve deserves review, not an automatic compromise verdict. It may have been installed locally, created by a maintainer script, or be an alternative or symlink path that dpkg records differently. Confirm its origin and purpose before changing its mode or deleting it.

Build a baseline for this host

There is no universal “stock Debian 12” list that every server should match. The result varies with installed packages, package versions, local policy, filesystem layout, and administrator changes. Create a baseline from the actual host, record the Debian release and installed package state alongside it, and review every entry with the system owner. Treat common package examples in third-party guides as leads to verify, not an allowlist.

For each file found, record its path, mode, owner, group, filesystem, package query result, and reason the special bit is needed. Confirm package ownership with dpkg-query -S -- <path> where possible, and check package contents or verification data as appropriate. A match establishes a package database association; it does not validate the current mode or content. For files without a match, check alternatives, maintainer scripts, local deployment manifests, and change records before deciding whether to remediate.

Finding every SUID and SGID file with find

For a root-filesystem inventory, scan as root, skip common virtual filesystems explicitly, and restrict traversal to the filesystem containing /:

Terminal window
sudo find / \
-path /proc -prune -o \
-path /sys -prune -o \
-path /run -prune -o \
-xdev -type f \( -perm -4000 -o -perm -2000 \) \
-printf '%M %u %g %p\n' 2>/dev/null

-perm -4000 matches files with the SUID bit set, regardless of the other permission bits; -perm -2000 does the same for SGID. GNU find evaluates -o as OR, so the parentheses group the two tests. -type f intentionally excludes SGID directories; audit those separately if they are in scope. -printf is a GNU extension available on Debian. Redirecting errors keeps output concise, but may hide incomplete traversal, so review mount scope and permissions rather than assuming the scan found every file.

-xdev limits traversal to the filesystem containing /; it does not scan other mounted filesystems. If those are in scope, remove -xdev only after deciding which mounts to include, and prune pseudo-filesystems, network mounts, removable media, and container mounts as appropriate. Check findmnt and document the exclusions.

If you want to inventory SGID directories, run a separate pass that omits -type f and selects the SGID bit. Interpret directory SGID as group-inheritance behavior, not executable privilege elevation; compare each result with the directory’s purpose and ownership.

Reading find’s permission notation correctly

GNU find accepts three forms of the -perm test, and they are not interchangeable.

-perm 4755 matches files whose mode is exactly 4755. A file at 4755 matches; a file at 6755 does not, even though it also has SUID. Do not use this form for an audit.

For GNU find, -perm -4000 requires the SUID bit while allowing other permission bits to vary; -perm -2000 does the same for SGID. Keep these tests separate with -o to select either bit. Consult the installed find(1) manual before using alternate -perm forms, since their semantics differ and portability matters.

Checking package ownership with dpkg-query

Use dpkg-query -S to ask which installed package records a path:

Terminal window
dpkg-query -S -- /usr/bin/passwd

Output identifies matching package records; exact formatting can include multiple packages or diversions. A lookup that returns no match needs investigation, but it is not proof of compromise. Debian’s documentation notes that dpkg-query -S does not list extra files created by maintainer scripts or alternatives. Keep the path and output with the audit record, then check the package manifest and local deployment history.

Package ownership is not a permission allowlist. A package may own the file while the current mode has been changed locally, or a package may install files through scripts that are not represented as ordinary manifest entries. Verify the file mode and content separately.

Inspecting files listed by a package

dpkg-query -L lists paths recorded for an installed package. Use it to select candidates, then inspect each path mode with stat. For example:

Terminal window
while IFS= read -r path; do
[ -e "$path" ] || continue
stat -c '%A %a %U %G %n' -- "$path"
done < <(dpkg-query -L -- sudo)

This is an inventory aid, not proof that the package expects any SUID or SGID bit shown. Review the package file list, maintainer scripts, vendor documentation, and installed version. Package path lists are newline-delimited.

To compare the filesystem scan against package records, save the scan as a NUL-delimited list and query each path. Do not split paths with an unquoted for loop, and do not treat a missing dpkg-query -S result as definitive:

Terminal window
sudo find / -path /proc -prune -o -path /sys -prune -o -path /run -prune -o \
-xdev -type f \( -perm -4000 -o -perm -2000 \) -print0 2>/dev/null > /var/tmp/suid-sgid.paths0
while IFS= read -r -d '' path; do
printf '\n%s\n' "$path"
dpkg-query -S -- "$path" || printf 'No dpkg path match; investigate provenance.\n'
done < /var/tmp/suid-sgid.paths0

dpkg-query -S uses package metadata and can omit files created by maintainer scripts, alternatives, or local deployment. Treat it as one source of provenance and compare the result with the trusted baseline.

Build and compare a baseline

A filesystem scan is a snapshot. Save a baseline only after reviewing the current results and recording the Debian release, package state, mount scope, and approved exceptions. Store it in a root-owned directory with restrictive permissions; an attacker who can replace both the files and baseline can hide changes.

Terminal window
sudo install -d -o root -g root -m 0700 /var/lib/suid-audit
sudo sh -c 'umask 077; find / -path /proc -prune -o -path /sys -prune -o -path /run -prune -o -xdev -type f \( -perm -4000 -o -perm -2000 \) -print0 2>/dev/null | xargs -0 -r sha256sum | sort > /var/lib/suid-audit/hashes.new'
sudo sh -c 'umask 077; find / -path /proc -prune -o -path /sys -prune -o -path /run -prune -o -xdev -type f \( -perm -4000 -o -perm -2000 \) -printf "%m %u %g %p\n" 2>/dev/null | sort > /var/lib/suid-audit/meta.new'

The .new files are candidates. Compare them with the approved baseline and investigate additions, removals, mode changes, and content changes. Do not replace the baseline until each difference has an explanation and the new state is reviewed. This prevents an alert run from silently teaching the audit to trust a suspicious change.

For a simple manual comparison after a baseline exists:

Terminal window
sudo diff -u /var/lib/suid-audit/hashes /var/lib/suid-audit/hashes.new || true
sudo diff -u /var/lib/suid-audit/meta /var/lib/suid-audit/meta.new || true

Content changes often follow package upgrades, but correlate them with package version changes and deployment records; do not assume that explanation. Metadata changes can alter the SUID/SGID bits, owner, or group without changing file content. After review, promote the candidates to baseline using a controlled, root-only process. Keep previous baselines in an access-controlled location if rollback or incident investigation requires them.

An automated monitor should preserve the previous approved baseline until an operator reviews alerts, report scan failures instead of treating partial output as clean, and deliver alerts through a tested channel. Avoid resetting the baseline automatically after every package upgrade: a compromised package or unexpected file could otherwise be accepted without review.

Checking package file contents with dpkg —verify

dpkg --verify compares file metadata recorded in the package database with installed files. In Debian Bookworm, the only currently functional check is an MD5 content comparison when a digest is present in the database. It does not verify SUID/SGID mode bits, ownership policy, or every package file.

Terminal window
sudo dpkg --verify

The default output format is described in the Bookworm dpkg(1) manual. It reports paths that fail an available check; the 5 position indicates a digest mismatch. A conffile may be marked with c. No output means no discrepancy was reported by the checks available to dpkg, not that the system is pristine or free of unexpected SUID/SGID files.

Use this as a separate content-integrity signal alongside the filesystem inventory and trusted baseline. Do not treat it as a substitute for checking permission bits, package ownership, or deployment records.

Inspecting a package archive

Before installing a locally obtained .deb, inspect its contents and metadata with the Debian package tools. dpkg-deb --info package.deb shows control metadata; dpkg-deb --contents package.deb lists archive paths and modes. Treat archive metadata as input to review, not as a complete security verdict. Confirm the package source, signature or repository provenance, maintainer scripts, and the files that will be installed. After installation, compare actual SUID/SGID modes with package documentation and your approved baseline.

Review mount options without disrupting workloads

A nosuid mount option prevents SUID and SGID bits from granting their usual privilege effect on that mounted filesystem. nodev prevents device files there from being used as devices. These options can be useful on filesystems writable by unprivileged users, but changing them may affect applications or operational workflows. Review each mount’s purpose, users, and applications before proposing a change; do not copy a generic /etc/fstab line across hosts.

Inspect the current mounts and effective options:

Terminal window
findmnt -o TARGET,SOURCE,FSTYPE,OPTIONS
findmnt -no OPTIONS /tmp
findmnt -no OPTIONS /var/tmp
findmnt -no OPTIONS /home

Compare the output with local policy and the distribution’s configuration. /tmp, /var/tmp, and /home may be separate mounts, bind mounts, or directories on /; a path lookup alone does not tell you which configuration controls it. If hardening is required, plan the change using the filesystem’s actual source and mount mechanism, validate /etc/fstab or the relevant systemd mount unit, and schedule a safe remount or reboot test. A typo in mount configuration can prevent a system from booting normally or make a service unavailable.

Do not add noexec by default: build tools, language runtimes, and installers may rely on executing files from temporary storage. Check workload requirements and test before applying additional restrictions. Verify the effective mount options again after the change.

Troubleshooting the inventory

If find reports traversal errors, treat the scan as incomplete. Review which filesystem or directory caused them, check mount scope, and adjust the explicit prune list or permissions. Do not suppress all errors and then assume the resulting list is complete; preserve a separate error log when completeness matters.

Place GNU find global options such as -xdev before tests and actions. Keep the grouped SUID/SGID predicates in parentheses so operator precedence is clear. Test the expression on a known file and inspect find --version if the target system differs from Debian’s GNU findutils.

If dpkg-query -S -- "$path" returns no match, record that result and investigate how the file was installed. Check symlink targets with readlink -f, alternatives with update-alternatives --query <name> when applicable, package maintainer scripts, and deployment records. dpkg-query -S is not a complete filesystem provenance database.

If dpkg reports missing or inconsistent package metadata, do not immediately reinstall packages on a production host. Preserve the current state, inspect dpkg logs and package status, and follow Debian package-management recovery guidance. Reinstallation may replace files or alter evidence; plan it as a change after confirming package origin and impact.

For find -perm, the leading - means all specified bits must be present, while / means any specified bit. To select SUID or SGID files, use -perm -4000 -o -perm -2000 as in the main scan, or -perm /6000 where supported by the GNU find version. Avoid combining extra permission bits unless you intend to require them too.

What this method does not catch

Three categories of elevated privilege do not show up in the find output.

File capabilities. Since Linux 2.2, setcap grants specific capabilities to a binary without the SUID bit. A binary with cap_net_raw+ep can create raw sockets without being SUID root. On Debian 12, ping in iputils-ping typically relies on the sysctl net.ipv4.ping_group_range rather than capabilities, but other binaries may not. Check with:

Terminal window
sudo getcap -r / 2>/dev/null

Add that output to the same baseline and diff process.

Access control lists. File ACLs can grant access beyond the traditional owner/group/other mode bits. An ACL does not itself grant SUID/SGID execution, but it can affect who may modify a privileged file or traverse its directories. When auditing sensitive paths, inspect ACLs with getfacl and include access-control changes in your review. Do not infer ACL coverage from the mode-only find output.

Package age and vulnerabilities. Package ownership does not establish that a binary is current or free of vulnerabilities. Check the installed package version against Debian security advisories and your update policy. Package updates and unattended-upgrades can help keep systems current, but confirm their configuration and logs rather than assuming updates were applied.

A worked investigation

Suppose a scan reports /opt/app/bin/helper with the SUID bit set. Treat this as a lead. Record its current mode, owner, group, filesystem, file hash, and package query result; compare those values with the approved baseline and deployment records. The example path is illustrative, not a real host finding.

If dpkg has no matching path record, check whether the application is installed by a deployment system, archive, or maintainer script. Review the deployment artifact and extraction method; do not assume a umask alone explains the resulting special bit, because archive tools and installation processes handle modes differently. Establish which component created the file and whether the special bit is required.

If the bit is not approved, plan a controlled remediation. Removing SUID from a file changes behavior and may break legitimate operation. Test the change, document the reason, and update the deployment process or package policy so the setting does not return unexpectedly. Do not run chmod on a live privileged binary solely because the package query did not find it.

A periodic review

Choose a review interval that fits the host’s risk and change rate, and repeat the inventory after deployments or package changes that may affect privileged files. Compare current output with a protected baseline, inspect package ownership and content verification separately, review file capabilities with getcap if installed, and assess mount options against local policy. Resolve and document each finding before updating the trusted baseline.

For a fleet, collect inventory results through a controlled configuration-management or monitoring system. Restrict access to the reports, preserve enough history for incident review, and ensure failed or partial scans are reported as failures rather than clean results.

The audit is a review of privileged file state, not a binary good/bad list. find shows which files currently carry SUID or SGID bits. dpkg-query provides one source of package provenance. Package metadata, deployment records, ACLs, content verification, and mount policy help explain whether each result is expected.

found this useful? share it →
Written by

Dzubayyan Ahmad

System Administrator, SRE & Technical Support Engineer specialising in web hosting infrastructure — Linux servers, automation, and monitoring. Writing these notes since 2015 so future-me can find the fix again.