Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

How SELinux deny rules improve system security

June 4, 2025
Petr Lautrbach
Related topics:
LinuxSecurity
Related products:
Red Hat Enterprise Linux

Share:

    SELinux userspace release 3.6 introduces deny rules. SELinux is designed as an additional layer of system security. SELinux policy defines the access and operations (e.g., read or write) allowed for certain resources. SELinux stops all access unless allowed by policy.

    Before the SELinux 3.6 userspace version, it was not possible to drop any access already allowed in the base SELinux policy or in a module. When you wanted to remove access, you had to rewrite the base policy or the module. This would mean maintaining your own policy or module and overriding the distribution policies. While it’s feasible, it costs resources and could be error-prone, because you would need to merge distribution changes with your own.

    How deny rules work

    The changes in the latest SELinux userspace release 3.6 introduced support for deny rules. They are documented in Access Vector Rules: "Remove the access rights defined from any matching allow rules. These rules are processed before neverallow checking."

    Rule definition:

    (deny source_id target_id|self classpermissionset_id ...)

    A deny rule is like a neverallow rule, except it removes permissions rather than reporting errors.

    Let’s consider the following use cases.

    Restricted users

    As an administrator, I need to limit confined users so that they cannot run the ssh command.

    The /usr/bin/ssh is labeled with the ssh_exec_t file context. By default, user_u users are allowed to execute files with this context:

    # sesearch -A -s user_t -t ssh_exec_t
    ...
    allow user_usertype application_exec_type:file { execute execute_no_trans getattr ioctl lock map open read };

    In order to block user_u users to execute them, we need to deny these permissions:

    # cat > deny_user_t_ssh_exec_t.cil <<EOF
    (deny user_t ssh_exec_t (file (execute execute_no_trans getattr ioctl lock map open read)))
    EOF
    # semodule -i deny_user_t_ssh_exec_t.cil

    If a user tries to use ssh now they get Permission denied:

    user@localhost:~$ /usr/bin/ssh localhost
    -bash: /usr/bin/ssh: Permission denied

    Restricted system admin

    As an administrator, I need to have another administrator assigned to sysadm_u who would not be able to read user homedir data.

    We’ll create a new user sysadm mapped to the SELinux user staff_u, allow them to become root by running sudo assysadm_t, and check whether they’re allowed to read homedir:

    # useradd -Z staff_u sysadm
    # passwd sysadm
    New password:
    BAD PASSWORD: The password is shorter than 8 characters
    Retype new password:
    passwd: password updated successfully
    # cat > /etc/sudoers.d/sysadm <<EOF
    sysadm ALL=(ALL) TYPE=sysadm_t ROLE=sysadm_r ALL
    EOF
    # ssh sysadm@localhost
    sysadm@localhost's password:
    sysadm@localhost:~$ id -Z
    staff_u:staff_r:staff_t:s0-s0:c0.c1023
    sysadm@localhost:~$ sudo -i
    [sudo] password for sysadm:
    root@localhost:~# id -Z
    staff_u:sysadm_r:sysadm_t:s0-s0:c0.c1023
    root@localhost:~# ls /home/
    bachradsusi  mock  plautrba  sysadm  user
    root@localhost:~# ls /home/user
    Desktop  Documents  Downloads  linux-system-roles-selinux  Music  Pictures  Public  Templates  Videos

    Apparently, sysadm can read other user homes so we need to drop permissions:

    # cat > deny_sysadm_user_home_dir_t.cil <<EOF
    (deny sysadm_t user_home_dir_t (dir ( add_name append audit_access create execmod execute getattr ioctl link lock mounton open quotaon read relabelfrom relabelto remove_name rename reparent rmdir search setattr swapon unlink watch watch_mount watch_reads watch_sb watch_with_perm write )))
    EOF
    # semodule -i deny_sysadm_user_home_dir_t.cil
    # sesearch -A -s sysadm_t -t user_home_dir_t -c dir
    #
    root@localhost:~# ls /home/user
    ls: cannot access '/home/user': Permission denied

    It looks good. But what happens if the user switches to permissive as follows:

    root@localhost:~# setenforce 0
    root@localhost:~# ls /home/user
    Desktop  Documents  Downloads  linux-system-roles-selinux  Music  Pictures  Public  Templates  Videos

    As you can see, a simple deny rule is not necessarily a good tool when used on privileged SELinux domains, which can manipulate SELinux settings on the hosts.

    Hardening the default policy

    Sometimes, the default SELinux policy allows users to hang a system. For example, when an unconfined user tries to ptrace fapolicyd. Although you could disallow ptrace for the whole system by using the deny_ptrace boolean, it’s not necessary to limit ptrace of other daemons. To limit ptrace and signals on fapolicyd, we can use a deny rule.

    This module is actually already shipped with the fapolicyd policy with PR add fapolicyd-hardening module preventing usage of sigstop, sigkill and ptrace.

    But you could do the same thing manually.

    # cat > deny_domain_fapolicyd_ptrace.cil <<EOF
    (deny domain fapolicyd_t ( process ( ptrace sigkill sigstop ) ) )
    EOF
    # semodule -i deny_domain_fapolicyd_ptrace

    Learn more

    SELinux userspace release 3.6 introduces deny rules. This article demonstrated how you can now remove SELinux permissions from the base SELinux policy or a module. This improves system security and reduces errors and costs.

    To learn more about SELinux, check out the SELinux documentation.

    Related Posts

    • How SELinux improves Red Hat Enterprise Linux security

    • How custom SELinux policies secure servers and containers

    • My advice on SELinux container labeling

    Recent Posts

    • Introducing Red Hat build of Cryostat 4.0

    • How we improved AI inference on macOS Podman containers

    • How OpenShift Virtualization supports VM live migration

    • How SELinux deny rules improve system security

    • Advanced time manipulation with GDB

    What’s up next?

    This cheat sheet provides a collection of Linux commands and executables for developers and system administrators looking to advance beyond the basics.

    Get the cheat sheet
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue