Introduction
In Linux, the /dev/shm directory is used for shared memory. It functions like a temporary file system stored in RAM, which makes it extremely fast.
Because it is backed by memory through tmpfs, /dev/shm behaves differently from normal disk-backed directories. It is meant for temporary, fast-access data exchange between processes.
However, /dev/shm often has permissions that allow any user to create files in it (similar to /tmp). This makes it a common target for malicious actors, who may try to hide and execute malicious scripts directly from memory to avoid detection.
To secure your system, you can restrict this directory so that files stored there cannot be executed. Mount options such as noexec are a classic Linux hardening technique: instead of trusting every writable location, you reduce what those locations are allowed to do.
In this lab, you will:
- Verify the vulnerability by running a script in
/dev/shm. - Secure the directory using the
noexecmount option. - Confirm that execution is now blocked.
Check Current Mount Options
First, let's examine the current configuration of the shared memory directory.
Use the mount command filtered by grep to see how /dev/shm is mounted.
mount | grep shm
You will likely see output similar to this: (rw,nosuid,nodev,inode64)
Notice that there is no noexec option. This means execution is allowed.
Mount options are a filesystem-level policy. They can allow or forbid behaviors such as executing files, using device files, or honoring setuid bits regardless of normal file permissions.
Demonstrate the Vulnerability
To understand the risk, let's create a simple script in this directory and try to run it.
First, create a script named payload.sh that prints a message:
echo -e '#!/bin/bash\necho "Malicious script executing..."' > /dev/shm/payload.sh
Then, make the script executable:
chmod +x /dev/shm/payload.sh
Finally, execute the script:
/dev/shm/payload.sh
Output:
Malicious script executing...
This confirms that currently, any user can execute programs located in shared memory.
That is the core security concern here. A writable location that also permits execution can become a staging area for unwanted code.
Secure Shared Memory with noexec
To prevent programs from running in /dev/shm, we need to add the noexec (No Execution) flag to its mount options.
We can do this without unmounting the filesystem by using the remount option.
Run the following command:
sudo mount -o remount,noexec /dev/shm
remount: Tells the system to modify the options of an already mounted filesystem.noexec: Instructs the kernel to refuse execution of any binaries or scripts in this filesystem.
This is a classic example of hardening through restriction. Instead of trusting users or processes not to misuse a writable location, you remove the capability entirely at the mount level.
If the command is successful, it will produce no output.
Verify the Security Configuration
Now that we have applied the security control, let's verify that it works.
Check Mount Options
Check the mount options again:
mount | grep shm
You should now see noexec in the list (e.g., rw,nosuid,nodev,noexec).
Test Execution Block
Let's try to run the script we created earlier.
First, ensure the script still exists (remounting usually preserves files, but good to be sure). If it's gone, recreate it:
echo -e '#!/bin/bash\necho "Malicious script executing..."' > /dev/shm/payload.sh
chmod +x /dev/shm/payload.sh
Then, attempt to execute it:
/dev/shm/payload.sh
Output:
zsh: permission denied: /dev/shm/payload.sh
Success! The system now blocks the execution. Even though the file has executable permissions (chmod +x), the filesystem mount option overrides it and prevents the code from running.
That final detail matters because security in Linux has multiple layers. File permissions are one layer, but mount options are another. The effective result comes from the combination of both.
Summary
You have successfully secured the shared memory on your system.
In this lab, you checked existing vulnerabilities and applied the noexec mount option to /dev/shm.
Key Takeaways:
- Shared Memory Risks: Directories like
/dev/shmare often world-writable, making them prime locations for malicious scripts. - Mount Options: Using
mount -o noexecis a powerful way to restrict what can happen in specific directories. - Defense in Depth: Even if a hacker manages to upload a file to your system, preventing its execution is a critical layer of defense.



