SlideShare a Scribd company logo
TLPI - Chapter 38
Writing Secure Privileged
Programs
Shu-Yu Fu (shuyufu@gmail.com)
In This Chapter
We discuss a set of recommended practices for
secure programming, and describes various
pitfalls that should be avoided when writing
privileged programs.
Privileged Programs
● have access to features and resources that
are not available to ordinary users.
● was started under a privileged user ID.
● has its set-user-ID or set-group-ID
permission bit set.
○ When a set-user-id (set-group-ID) program is
execed, it changes the effective user (group) ID of
the process to be the same as the owner (group) of
the program file.
Is a Set-User-ID or Set-Group-ID
Program Required?
● Avoid writing them whenever possible.
● Is there an alternative way?
● Isolate the functionality that needs privilege
into a separate program.
● It isn't always necessary for a set-user-ID
program to give a process root credentials.
● A dedicated group account (group ID) for the
program.
Operate with Least Privilege
● The saved set-user-ID facility was designed
for this purpose.
uid_t orig_euid;
orig_euid = geteuid();
/* Drop privileges */
if (seteuid(getuid()) == -1) errExit("seteuid");
/* Do unprivileged work*/
/* Hold privileges only while they are required. */
/* Reacquire privileges */
if (seteuid(orig_euid) == -1) errExit("seteuid");
/* Do privileged work*/
/* We must gain privilege prior to dropping it permanently */
/* Drop privileges permanently when they will never agin be required. */
/* Use setuid() for set-user-root and setreuid() (or setresuid()) for set-user-ID
program */
if (setuid(getuid() == -1) errExit("setuid");
Operate with Least Privilege (cont.)
● [Tsafrir et al., 2008] recommends that
applications should use system specific
nonstandard system calls for changing
process credentials.
● Not only check that a credential-changing
system call has succeeded, but also to verify
that the change occurred as expected.
● We should drop/raise the privileged effective
user ID last/first when dropping/raising
privileged IDs.
Be Careful When Executing a
Program
● Drop privileges permanently before execing
another program.
● Avoid executing a shell (or other interpreter)
with privileges.
● Close all unnecessary file descriptors before
an exec().
Avoid Exposing Sensitive
Information
● When a program reads sensitive information,
it should perform whatever processing is
required, and then immediately erase the
information from memory.
○ The virtual memory page may be swapped out, and
could then be read from the swap area by a
privileged program.
○ If the process receives a signal that causes it to
produce a core dump file, then that file may be read
to obtain the information.
Confine the Process
● Consider using capabilities
The Linux capabilities scheme divides the
traditional all-or-nothing UNIX privilege
scheme into distinct units called capabilities.
● Consider using a chroot jail to limit the set of
directories and files that a program may
access. However, a chroot jail in insufficient
to confine a set-user-ID-root program.
Beware of Signals and Race
Conditions
● We need to consider the race conditions that
can occur if a signal is delivered at any point
in the execution of the program.
○ Time-of-check, time-of-use race condition.
Pitfalls When Performing File
Operations and File I/O
● The process umask should be set to a value that
ensures that the process never creates publicly writable
files.
● Use of seteuid() or setreuid() may required in order
ensure that a newly created file doesn't belong to the
wrong user.
● Checks on file attributes should be performed on open
file descriptors. (Avoid time-of-use, time-of-check
problem)
● If a program must ensure that it is the creator of a file,
then the O_EXCL flag should be used when calling
open().
● A privileged program should avoid creating or relying on
files in publicly writable directories.
Don't Trust Inputs or the
Environment
● Don't trust the environment list.
○ Especially PATH and IFS.
● Handle untrusted user inputs defensively.
○ User-created files, command-line arguments,
interactive inputs, CGI inputs, email messages,
environment variables, IPC (FIFOs, shared memory,
and so on) accessible by untrusted users, and
network packets.
● Avoid unreliable assumptions about the
process's run-time environment.
Beware of Buffer Overruns
● Never use gets(), and employ functions such
as scanf(), sprintf(), strcpy(), and strcat() with
caution.
○ stack crashing (a.k.a. stack smashing)
■ address-space-randomization
■ NX (no execute)
● Alternatives, snprintf(), strncpy(), and strncat().
○ The caller must check if truncation occurred.
○ Using strncpy() can carry a performance impact.
○ If the maximum size value given to strncpy() is not
long enough to permit the inclusion of the
terminating null character, the target string is not
null-terminated.
Beware of Denial-of-Service Attacks
● Denial-of-service attacks attempt to make a
service unavailable to legitimate clients.
○ A server should be programmed to rigorously check its input
and avoid buffer overruns.
○ The Server should perform load throttling.
○ A server should employ timeouts for communication with a
client. SYN flood
○ In the event of an overload, the server should log suitable
messages. However, logging should be throttled.
○ The server should be programmed so that it doesn't crash in
the face of an unexpected load.
○ Data structures should be designed to avoid algorithmic-
complexity attacks.
○ Spoofing attack
Check Return Statuses and Fail
Safely
● A program should always check to see
whether system calls and library functions
succeed, and whether they return expected
values.
● If a privileged program encounters an
unexpected situation, then the appropriate
behavior is usually either to terminate or, in
the case of a server, to drop the client
request.
Summary
In this chapter, we presented a set of
guidelines for writing privileged programs. The
aim of these guidelines is twofold: to minimize
the chances of a privileged program being
subverted, and to minimize the damage that
can be done in the event that a privileged
program is subverted.
Reference
● Oracle LinuxSecurity Guide for Release 6,
Chapter 4. Security Considerations for
Developers
● Secure Programming for Linux and Unix
HOWTO

More Related Content

Similar to Tlpi chapter 38 writing secure privileged programs

Automotive Grade Linux and systemd
Automotive Grade Linux and systemdAutomotive Grade Linux and systemd
Automotive Grade Linux and systemd
Alison Chaiken
 
CISSP Week 14
CISSP Week 14CISSP Week 14
CISSP Week 14
jemtallon
 
Ever Present Persistence - Established Footholds Seen in the Wild
Ever Present Persistence - Established Footholds Seen in the WildEver Present Persistence - Established Footholds Seen in the Wild
Ever Present Persistence - Established Footholds Seen in the Wild
CTruncer
 
Platform Security IRL: Busting Buzzwords & Building Better
Platform Security IRL:  Busting Buzzwords & Building BetterPlatform Security IRL:  Busting Buzzwords & Building Better
Platform Security IRL: Busting Buzzwords & Building Better
Equal Experts
 
Liferay portals in real projects
Liferay portals  in real projectsLiferay portals  in real projects
Liferay portals in real projects
IBACZ
 
Mitigating Java Deserialization attacks from within the JVM
Mitigating Java Deserialization attacks from within the JVMMitigating Java Deserialization attacks from within the JVM
Mitigating Java Deserialization attacks from within the JVM
Apostolos Giannakidis
 
Ron Munitz - The Ultimate Android Security Checklist - Codemotion Rome 2015
Ron Munitz - The Ultimate Android Security Checklist - Codemotion Rome 2015Ron Munitz - The Ultimate Android Security Checklist - Codemotion Rome 2015
Ron Munitz - The Ultimate Android Security Checklist - Codemotion Rome 2015
Codemotion
 
DevSecOps: What Why and How : Blackhat 2019
DevSecOps: What Why and How : Blackhat 2019DevSecOps: What Why and How : Blackhat 2019
DevSecOps: What Why and How : Blackhat 2019
NotSoSecure Global Services
 
Thick Client Testing Basics
Thick Client Testing BasicsThick Client Testing Basics
Thick Client Testing Basics
NSConclave
 
Apache flink
Apache flinkApache flink
Apache flink
pranay kumar
 
Stability Patterns for Microservices
Stability Patterns for MicroservicesStability Patterns for Microservices
Stability Patterns for Microservices
pflueras
 
CISSP Week 13
CISSP Week 13CISSP Week 13
CISSP Week 13
jemtallon
 
The Supporting Role of Antivirus Evasion while Persisting
The Supporting Role of Antivirus Evasion while PersistingThe Supporting Role of Antivirus Evasion while Persisting
The Supporting Role of Antivirus Evasion while Persisting
CTruncer
 
Security Design Principles.ppt
 Security Design Principles.ppt Security Design Principles.ppt
Security Design Principles.ppt
DrBasemMohamedElomda
 
Lecture_02_System_Structures.ppt.pdf
Lecture_02_System_Structures.ppt.pdfLecture_02_System_Structures.ppt.pdf
Lecture_02_System_Structures.ppt.pdf
ssuserf3a6ff
 
Security .NET.pdf
Security .NET.pdfSecurity .NET.pdf
Security .NET.pdf
Abhi Jain
 
AWS Well-Architected Framework (nov 2017)
AWS Well-Architected Framework (nov 2017)AWS Well-Architected Framework (nov 2017)
AWS Well-Architected Framework (nov 2017)
Rick Hwang
 
Answers
AnswersAnswers
Security of Oracle EBS - How I can Protect my System (UKOUG APPS 18 edition)
Security of Oracle EBS - How I can Protect my System (UKOUG APPS 18 edition)Security of Oracle EBS - How I can Protect my System (UKOUG APPS 18 edition)
Security of Oracle EBS - How I can Protect my System (UKOUG APPS 18 edition)
Andrejs Prokopjevs
 
Pluggable Infrastructure with CI/CD and Docker
Pluggable Infrastructure with CI/CD and DockerPluggable Infrastructure with CI/CD and Docker
Pluggable Infrastructure with CI/CD and Docker
Bob Killen
 

Similar to Tlpi chapter 38 writing secure privileged programs (20)

Automotive Grade Linux and systemd
Automotive Grade Linux and systemdAutomotive Grade Linux and systemd
Automotive Grade Linux and systemd
 
CISSP Week 14
CISSP Week 14CISSP Week 14
CISSP Week 14
 
Ever Present Persistence - Established Footholds Seen in the Wild
Ever Present Persistence - Established Footholds Seen in the WildEver Present Persistence - Established Footholds Seen in the Wild
Ever Present Persistence - Established Footholds Seen in the Wild
 
Platform Security IRL: Busting Buzzwords & Building Better
Platform Security IRL:  Busting Buzzwords & Building BetterPlatform Security IRL:  Busting Buzzwords & Building Better
Platform Security IRL: Busting Buzzwords & Building Better
 
Liferay portals in real projects
Liferay portals  in real projectsLiferay portals  in real projects
Liferay portals in real projects
 
Mitigating Java Deserialization attacks from within the JVM
Mitigating Java Deserialization attacks from within the JVMMitigating Java Deserialization attacks from within the JVM
Mitigating Java Deserialization attacks from within the JVM
 
Ron Munitz - The Ultimate Android Security Checklist - Codemotion Rome 2015
Ron Munitz - The Ultimate Android Security Checklist - Codemotion Rome 2015Ron Munitz - The Ultimate Android Security Checklist - Codemotion Rome 2015
Ron Munitz - The Ultimate Android Security Checklist - Codemotion Rome 2015
 
DevSecOps: What Why and How : Blackhat 2019
DevSecOps: What Why and How : Blackhat 2019DevSecOps: What Why and How : Blackhat 2019
DevSecOps: What Why and How : Blackhat 2019
 
Thick Client Testing Basics
Thick Client Testing BasicsThick Client Testing Basics
Thick Client Testing Basics
 
Apache flink
Apache flinkApache flink
Apache flink
 
Stability Patterns for Microservices
Stability Patterns for MicroservicesStability Patterns for Microservices
Stability Patterns for Microservices
 
CISSP Week 13
CISSP Week 13CISSP Week 13
CISSP Week 13
 
The Supporting Role of Antivirus Evasion while Persisting
The Supporting Role of Antivirus Evasion while PersistingThe Supporting Role of Antivirus Evasion while Persisting
The Supporting Role of Antivirus Evasion while Persisting
 
Security Design Principles.ppt
 Security Design Principles.ppt Security Design Principles.ppt
Security Design Principles.ppt
 
Lecture_02_System_Structures.ppt.pdf
Lecture_02_System_Structures.ppt.pdfLecture_02_System_Structures.ppt.pdf
Lecture_02_System_Structures.ppt.pdf
 
Security .NET.pdf
Security .NET.pdfSecurity .NET.pdf
Security .NET.pdf
 
AWS Well-Architected Framework (nov 2017)
AWS Well-Architected Framework (nov 2017)AWS Well-Architected Framework (nov 2017)
AWS Well-Architected Framework (nov 2017)
 
Answers
AnswersAnswers
Answers
 
Security of Oracle EBS - How I can Protect my System (UKOUG APPS 18 edition)
Security of Oracle EBS - How I can Protect my System (UKOUG APPS 18 edition)Security of Oracle EBS - How I can Protect my System (UKOUG APPS 18 edition)
Security of Oracle EBS - How I can Protect my System (UKOUG APPS 18 edition)
 
Pluggable Infrastructure with CI/CD and Docker
Pluggable Infrastructure with CI/CD and DockerPluggable Infrastructure with CI/CD and Docker
Pluggable Infrastructure with CI/CD and Docker
 

More from Shu-Yu Fu

團隊之美 第三篇 實踐 (Part 1)
團隊之美 第三篇 實踐 (Part 1)團隊之美 第三篇 實踐 (Part 1)
團隊之美 第三篇 實踐 (Part 1)Shu-Yu Fu
 
團隊之美 第二篇 目標
團隊之美 第二篇 目標團隊之美 第二篇 目標
團隊之美 第二篇 目標Shu-Yu Fu
 
TLPI - 7 Memory Allocation
TLPI - 7 Memory AllocationTLPI - 7 Memory Allocation
TLPI - 7 Memory Allocation
Shu-Yu Fu
 
程式設計師的自我修養 Chapter 10 記憶體
程式設計師的自我修養 Chapter 10 記憶體程式設計師的自我修養 Chapter 10 記憶體
程式設計師的自我修養 Chapter 10 記憶體Shu-Yu Fu
 
大家來學GObject
大家來學GObject大家來學GObject
大家來學GObjectShu-Yu Fu
 
程式設計師的自我修養 Chapter 3.4
程式設計師的自我修養 Chapter 3.4程式設計師的自我修養 Chapter 3.4
程式設計師的自我修養 Chapter 3.4Shu-Yu Fu
 
程式設計師的自我修養 Chapter 8
程式設計師的自我修養 Chapter 8程式設計師的自我修養 Chapter 8
程式設計師的自我修養 Chapter 8Shu-Yu Fu
 

More from Shu-Yu Fu (7)

團隊之美 第三篇 實踐 (Part 1)
團隊之美 第三篇 實踐 (Part 1)團隊之美 第三篇 實踐 (Part 1)
團隊之美 第三篇 實踐 (Part 1)
 
團隊之美 第二篇 目標
團隊之美 第二篇 目標團隊之美 第二篇 目標
團隊之美 第二篇 目標
 
TLPI - 7 Memory Allocation
TLPI - 7 Memory AllocationTLPI - 7 Memory Allocation
TLPI - 7 Memory Allocation
 
程式設計師的自我修養 Chapter 10 記憶體
程式設計師的自我修養 Chapter 10 記憶體程式設計師的自我修養 Chapter 10 記憶體
程式設計師的自我修養 Chapter 10 記憶體
 
大家來學GObject
大家來學GObject大家來學GObject
大家來學GObject
 
程式設計師的自我修養 Chapter 3.4
程式設計師的自我修養 Chapter 3.4程式設計師的自我修養 Chapter 3.4
程式設計師的自我修養 Chapter 3.4
 
程式設計師的自我修養 Chapter 8
程式設計師的自我修養 Chapter 8程式設計師的自我修養 Chapter 8
程式設計師的自我修養 Chapter 8
 

Recently uploaded

Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
SitimaJohn
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
David Brossard
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 

Recently uploaded (20)

Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 

Tlpi chapter 38 writing secure privileged programs

  • 1. TLPI - Chapter 38 Writing Secure Privileged Programs Shu-Yu Fu (shuyufu@gmail.com)
  • 2. In This Chapter We discuss a set of recommended practices for secure programming, and describes various pitfalls that should be avoided when writing privileged programs.
  • 3. Privileged Programs ● have access to features and resources that are not available to ordinary users. ● was started under a privileged user ID. ● has its set-user-ID or set-group-ID permission bit set. ○ When a set-user-id (set-group-ID) program is execed, it changes the effective user (group) ID of the process to be the same as the owner (group) of the program file.
  • 4. Is a Set-User-ID or Set-Group-ID Program Required? ● Avoid writing them whenever possible. ● Is there an alternative way? ● Isolate the functionality that needs privilege into a separate program. ● It isn't always necessary for a set-user-ID program to give a process root credentials. ● A dedicated group account (group ID) for the program.
  • 5. Operate with Least Privilege ● The saved set-user-ID facility was designed for this purpose. uid_t orig_euid; orig_euid = geteuid(); /* Drop privileges */ if (seteuid(getuid()) == -1) errExit("seteuid"); /* Do unprivileged work*/ /* Hold privileges only while they are required. */ /* Reacquire privileges */ if (seteuid(orig_euid) == -1) errExit("seteuid"); /* Do privileged work*/ /* We must gain privilege prior to dropping it permanently */ /* Drop privileges permanently when they will never agin be required. */ /* Use setuid() for set-user-root and setreuid() (or setresuid()) for set-user-ID program */ if (setuid(getuid() == -1) errExit("setuid");
  • 6. Operate with Least Privilege (cont.) ● [Tsafrir et al., 2008] recommends that applications should use system specific nonstandard system calls for changing process credentials. ● Not only check that a credential-changing system call has succeeded, but also to verify that the change occurred as expected. ● We should drop/raise the privileged effective user ID last/first when dropping/raising privileged IDs.
  • 7. Be Careful When Executing a Program ● Drop privileges permanently before execing another program. ● Avoid executing a shell (or other interpreter) with privileges. ● Close all unnecessary file descriptors before an exec().
  • 8. Avoid Exposing Sensitive Information ● When a program reads sensitive information, it should perform whatever processing is required, and then immediately erase the information from memory. ○ The virtual memory page may be swapped out, and could then be read from the swap area by a privileged program. ○ If the process receives a signal that causes it to produce a core dump file, then that file may be read to obtain the information.
  • 9. Confine the Process ● Consider using capabilities The Linux capabilities scheme divides the traditional all-or-nothing UNIX privilege scheme into distinct units called capabilities. ● Consider using a chroot jail to limit the set of directories and files that a program may access. However, a chroot jail in insufficient to confine a set-user-ID-root program.
  • 10. Beware of Signals and Race Conditions ● We need to consider the race conditions that can occur if a signal is delivered at any point in the execution of the program. ○ Time-of-check, time-of-use race condition.
  • 11. Pitfalls When Performing File Operations and File I/O ● The process umask should be set to a value that ensures that the process never creates publicly writable files. ● Use of seteuid() or setreuid() may required in order ensure that a newly created file doesn't belong to the wrong user. ● Checks on file attributes should be performed on open file descriptors. (Avoid time-of-use, time-of-check problem) ● If a program must ensure that it is the creator of a file, then the O_EXCL flag should be used when calling open(). ● A privileged program should avoid creating or relying on files in publicly writable directories.
  • 12. Don't Trust Inputs or the Environment ● Don't trust the environment list. ○ Especially PATH and IFS. ● Handle untrusted user inputs defensively. ○ User-created files, command-line arguments, interactive inputs, CGI inputs, email messages, environment variables, IPC (FIFOs, shared memory, and so on) accessible by untrusted users, and network packets. ● Avoid unreliable assumptions about the process's run-time environment.
  • 13. Beware of Buffer Overruns ● Never use gets(), and employ functions such as scanf(), sprintf(), strcpy(), and strcat() with caution. ○ stack crashing (a.k.a. stack smashing) ■ address-space-randomization ■ NX (no execute) ● Alternatives, snprintf(), strncpy(), and strncat(). ○ The caller must check if truncation occurred. ○ Using strncpy() can carry a performance impact. ○ If the maximum size value given to strncpy() is not long enough to permit the inclusion of the terminating null character, the target string is not null-terminated.
  • 14. Beware of Denial-of-Service Attacks ● Denial-of-service attacks attempt to make a service unavailable to legitimate clients. ○ A server should be programmed to rigorously check its input and avoid buffer overruns. ○ The Server should perform load throttling. ○ A server should employ timeouts for communication with a client. SYN flood ○ In the event of an overload, the server should log suitable messages. However, logging should be throttled. ○ The server should be programmed so that it doesn't crash in the face of an unexpected load. ○ Data structures should be designed to avoid algorithmic- complexity attacks. ○ Spoofing attack
  • 15. Check Return Statuses and Fail Safely ● A program should always check to see whether system calls and library functions succeed, and whether they return expected values. ● If a privileged program encounters an unexpected situation, then the appropriate behavior is usually either to terminate or, in the case of a server, to drop the client request.
  • 16. Summary In this chapter, we presented a set of guidelines for writing privileged programs. The aim of these guidelines is twofold: to minimize the chances of a privileged program being subverted, and to minimize the damage that can be done in the event that a privileged program is subverted.
  • 17. Reference ● Oracle LinuxSecurity Guide for Release 6, Chapter 4. Security Considerations for Developers ● Secure Programming for Linux and Unix HOWTO