bsdcapsicum.rb/README.md

127 lines
3.3 KiB
Markdown
Raw Normal View History

2017-05-24 02:18:05 +02:00
# Capsicum
2017-05-24 17:34:00 +02:00
A simple FFI wrapper around the [Capsicum](https://wiki.freebsd.org/Capsicum)
OS capability and sandbox framework.
2017-05-24 02:18:05 +02:00
## Installation
2017-05-24 17:34:00 +02:00
A Capsicum-enabled OS is, of course, required. FreeBSD 10+ (or derivative),
possibly [capsicum-linux](http://capsicum-linux.org/).
2017-05-24 02:18:05 +02:00
Add this line to your application's Gemfile:
```ruby
gem 'capsicum'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install capsicum
2017-05-24 17:34:00 +02:00
2017-05-24 02:18:05 +02:00
## Usage
2017-05-24 02:18:47 +02:00
Basic synopsis:
```ruby
2024-06-25 04:36:09 +02:00
require "capsicum"
print "In capability mode: ", Capsicum.in_capability_mode? ? "yes" : "no", "\n"
print "Enter capability mode: ", Capsicum.enter! ? "ok" : "error", "\n"
print "In capability mode: ", Capsicum.in_capability_mode? ? "yes" : "no", "\n"
begin
File.new(File::NULL)
rescue Errno::ECAPMODE => ex
print "Error: #{ex.message} (#{ex.class})", "\n"
end
##
# In capability mode: no
# Enter capability mode: ok
# In capability mode: yes
# Error: Not permitted in capability mode @ rb_sysopen - /dev/null (Errno::ECAPMODE)
2017-05-24 02:18:47 +02:00
```
i.e. anything that involves opening a file, connecting a socket, or executing a
program is verboten. Kinda.
On fork-capable Rubies, you can also do this:
```ruby
2024-06-25 05:23:16 +02:00
require "capsicum"
print "[parent] In capability mode: ", Capsicum.in_capability_mode? ? "yes" : "no", "\n"
fork do
2024-06-25 05:23:16 +02:00
print "[subprocess] Enter capability mode: ", Capsicum.enter! ? "ok" : "error", "\n"
print "[subprocess] In capability mode: ", Capsicum.in_capability_mode? ? "yes" : "no", "\n"
print "[subprocess] Exit", "\n"
2017-05-24 02:18:47 +02:00
exit 42
end
Process.wait
2024-06-25 05:23:16 +02:00
print "[parent] In capability mode: ", Capsicum.in_capability_mode? ? "yes" : "no", "\n"
2017-05-24 02:18:47 +02:00
2024-06-25 05:23:16 +02:00
##
# [parent] In capability mode: no
# [subprocess] Enter capability mode: ok
# [subprocess] In capability mode: yes
# [subprocess] Exit
# [parent] In capability mode: no
2017-05-24 02:18:47 +02:00
```
2017-05-26 00:45:32 +02:00
## But How Can I Get Anything Done?
2017-05-24 02:18:47 +02:00
2024-06-25 04:00:21 +02:00
Open your files and sockets before the current process enters capability mode.
If you have a `TCPServer` open, for example, you can still call `#accept` on it,
so a useful server could conceivably run within it.
2017-05-24 02:18:47 +02:00
You *can* open new files, but this requires access to *at() syscalls. If Ruby
supported them, it might look something like this:
```ruby
dir = Dir.open("/path/to/my/files")
Capsicum.enter!
file = File.openat(dir, "mylovelyfile")
File.renameat(dir, "foo", dir, "bar")
File.unlinkat(dir, "moo")
```
2017-05-24 17:34:00 +02:00
Unfortunately, it doesn't. See https://bugs.ruby-lang.org/issues/10181
You may consider spawning off workers, maintaining a privileged master process,
and using IPC to communicate with them.
2017-05-24 02:18:47 +02:00
## Todo
Wrap Casper to provide DNS services, additional rights controls, etc.
2017-05-24 02:18:05 +02:00
2017-05-24 17:34:00 +02:00
2017-05-24 02:18:05 +02:00
## Development
2017-05-24 02:18:47 +02:00
After checking out the repo, run `bin/setup` to install dependencies. Then, run
`rake test` to run the tests. You can also run `bin/console` for an interactive
prompt that will allow you to experiment.
2017-05-24 02:18:05 +02:00
2017-05-24 02:18:47 +02:00
To install this gem onto your local machine, run `bundle exec rake install`. To
release a new version, update the version number in `version.rb`, and then run
`bundle exec rake release`, which will create a git tag for the version, push
git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
2017-05-24 02:18:05 +02:00
2017-05-24 17:34:00 +02:00
2017-05-24 02:18:05 +02:00
## Contributing
2017-05-24 17:34:00 +02:00
Bug reports and pull requests are welcome on GitHub at https://github.com/Freaky/ruby-capsicum.
2017-05-24 02:18:05 +02:00
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).