bsdcapsicum.rb/README.md

101 lines
2.6 KiB
Markdown
Raw Normal View History

2017-05-24 02:18:05 +02:00
# Capsicum
2017-05-24 02:18:47 +02:00
A simple FFI wrapper around the Capsicum OS capability and sandbox framework.
2017-05-24 02:18:05 +02:00
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'capsicum'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install capsicum
## Usage
2017-05-24 02:18:47 +02:00
Basic synopsis:
```ruby
Capsicum.sandboxed? # => false
Capsicum.enter! # => true
Capsicum.sandboxed? # => true
File.new("/dev/null") # => Errno::ECAPMODE: Not permitted in capability mode @ rb_sysopen - /dev/null
TCPSocket.new("0", 80) # => Errno::ECAPMODE: Not permitted in capability mode - connect(2) for "0" port 80
`rm -rf /` # => Errno::ECAPMODE: Not permitted in capability mode - rm
system "rm -rf /" # => nil
require 'time' # => LoadError: cannot load such file -- time
```
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
Capsicum.sandboxed? # => false
status = Capsicum.within_sandbox do
Capsicum.sandboxed? # => true
exit 42
end
Capsicum.sandboxed? # => false
status.exitstatus # => 42
```
The result is a Process::Status object.
## But How Can I get Anything Done?
Open your files and sockets before entering the sandbox. If you have a
`TCPServer` open, for example, you can still call `#accept` on it, so a useful
server could conceivably run within it.
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")
```
This is Ruby issue #10181: https://bugs.ruby-lang.org/issues/10181
## Todo
Wrap Casper to provide DNS services, additional rights controls, etc.
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
## Contributing
2017-05-24 02:18:47 +02:00
Bug reports and pull requests are welcome on GitHub at https://github.com/Freaky/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).