Skip to content

Commit

Permalink
Updated docs for privilege separation (*nix only)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethkalmer committed Aug 12, 2009
1 parent ea34f35 commit b2adc5d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
22 changes: 22 additions & 0 deletions Configuration.txt
Expand Up @@ -78,3 +78,25 @@ Or the same in +config/environment.rb+

# ...
end

=== Privilege Separation

By default daemon processes run as the user that starts them, inheriting all
their privileges (or lack thereof). Getting daemon-kit to drop privileges
can currently only be done using command-line parameters, and only works
reliable on *nix (OSX seemed cranky at the time of testing).

$ ./bin/daemon start --config user=nobody --config group=nobody

Privileges are dropped at the earliest possible phase of starting the daemon.

Things to note on privilege separation:

* You generally have to be root to be able to perform this
* File system permissions for +log/+ needs to be correct
* Daemon-kit will only shed privileges on the +start+ command, not on +run+
* Make sure your code is secure if accepting stuff from the outside world

The implementation stems from the advice given by Joe Damato on his blog post
http://timetobleed.com/tag/privilege-escalation/

1 change: 1 addition & 0 deletions History.txt
Expand Up @@ -4,6 +4,7 @@
* Allow process umask to be configured, defaults to 022
* Updates to DaemonKit::Config hashes
* Fixed argument parsing bug (reported by Mathijs Kwik (bluescreen303)
* Support for privilege separation (See Configuration.txt)

== 0.1.7.9 2009-06-22

Expand Down
16 changes: 8 additions & 8 deletions lib/daemon_kit/application.rb
Expand Up @@ -164,14 +164,6 @@ def redirect_io( simulate = false )
end

def drop_privileges
if DaemonKit.configuration.user
begin
user = Etc.getpwnam( DaemonKit.configuration.user )
Process::Sys.setuid( user.uid.to_i )
rescue => e
$stderr.puts "Caught exception while trying to drop user privileges: #{e.message}"
end
end
if DaemonKit.configuration.group
begin
group = Etc.getgrnam( DaemonKit.configuration.group )
Expand All @@ -180,6 +172,14 @@ def drop_privileges
$stderr.puts "Caught exception while trying to drop group privileges: #{e.message}"
end
end
if DaemonKit.configuration.user
begin
user = Etc.getpwnam( DaemonKit.configuration.user )
Process::Sys.setuid( user.uid.to_i )
rescue => e
$stderr.puts "Caught exception while trying to drop user privileges: #{e.message}"
end
end
end
end

Expand Down
8 changes: 8 additions & 0 deletions lib/daemon_kit/initializer.rb
Expand Up @@ -111,6 +111,14 @@ def after_daemonize
set_process_name

DaemonKit.logger.info( "DaemonKit (#{DaemonKit::VERSION}) booted, now running #{DaemonKit.configuration.daemon_name}" )

if DaemonKit.configuration.user || DaemonKit.configuration.group
euid = Process.euid
egid = Process.egid
uid = Process.uid
gid = Process.gid
DaemonKit.logger.info( "DaemonKit dropped privileges to: #{euid} (EUID), #{egid} (EGID), #{uid} (UID), #{gid} (GID)" )
end
end

def set_load_path
Expand Down

0 comments on commit b2adc5d

Please sign in to comment.