capsicum 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +112 -0
- data/Rakefile +10 -0
- data/bin/console +7 -0
- data/bin/setup +8 -0
- data/capsicum.gemspec +37 -0
- data/lib/capsicum.rb +71 -0
- data/lib/capsicum/version.rb +3 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3e5799b3a6ff47316dbc821b3adfc49926eb4929
|
4
|
+
data.tar.gz: e85c0fbdae996894169ff9ca79f95e9bc21f3b81
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f838a9d42d998fefdb2f334e79912b41612a9ae41c46efa3a52d559cc13c1d7d6c9f64dcc3e2c84d076c8a304d81c39efd8cd8c01ca943208cb411e50539e23c
|
7
|
+
data.tar.gz: 5f530f73ea30050fc8bbad4539f32ba435346cc7fa2e17fab8e8efe08bb40621f038e961e5f2a1e08e03fe4a904299a1af7bd7f8ff8111d3b481eb8c5259b60b
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Thomas Hurst
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
# Capsicum
|
2
|
+
|
3
|
+
A simple FFI wrapper around the [Capsicum](https://wiki.freebsd.org/Capsicum)
|
4
|
+
OS capability and sandbox framework.
|
5
|
+
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
A Capsicum-enabled OS is, of course, required. FreeBSD 10+ (or derivative),
|
10
|
+
possibly [capsicum-linux](http://capsicum-linux.org/).
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'capsicum'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install capsicum
|
25
|
+
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Basic synopsis:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
Capsicum.sandboxed? # => false
|
33
|
+
Capsicum.enter! # => true
|
34
|
+
Capsicum.sandboxed? # => true
|
35
|
+
|
36
|
+
File.new("/dev/null") # => Errno::ECAPMODE: Not permitted in capability mode @ rb_sysopen - /dev/null
|
37
|
+
TCPSocket.new("0", 80) # => Errno::ECAPMODE: Not permitted in capability mode - connect(2) for "0" port 80
|
38
|
+
`rm -rf /` # => Errno::ECAPMODE: Not permitted in capability mode - rm
|
39
|
+
system "rm -rf /" # => nil
|
40
|
+
require 'time' # => LoadError: cannot load such file -- time
|
41
|
+
```
|
42
|
+
|
43
|
+
i.e. anything that involves opening a file, connecting a socket, or executing a
|
44
|
+
program is verboten. Kinda.
|
45
|
+
|
46
|
+
On fork-capable Rubies, you can also do this:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
Capsicum.sandboxed? # => false
|
50
|
+
|
51
|
+
status = Capsicum.within_sandbox do
|
52
|
+
Capsicum.sandboxed? # => true
|
53
|
+
exit 42
|
54
|
+
end
|
55
|
+
|
56
|
+
Capsicum.sandboxed? # => false
|
57
|
+
status.exitstatus # => 42
|
58
|
+
```
|
59
|
+
|
60
|
+
The result is a Process::Status object.
|
61
|
+
|
62
|
+
|
63
|
+
## But How Can I get Anything Done?
|
64
|
+
|
65
|
+
Open your files and sockets before entering the sandbox. If you have a
|
66
|
+
`TCPServer` open, for example, you can still call `#accept` on it, so a useful
|
67
|
+
server could conceivably run within it.
|
68
|
+
|
69
|
+
You *can* open new files, but this requires access to *at() syscalls. If Ruby
|
70
|
+
supported them, it might look something like this:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
dir = Dir.open("/path/to/my/files")
|
74
|
+
|
75
|
+
Capsicum.enter!
|
76
|
+
|
77
|
+
file = File.openat(dir, "mylovelyfile")
|
78
|
+
File.renameat(dir, "foo", dir, "bar")
|
79
|
+
File.unlinkat(dir, "moo")
|
80
|
+
```
|
81
|
+
|
82
|
+
Unfortunately, it doesn't. See https://bugs.ruby-lang.org/issues/10181
|
83
|
+
|
84
|
+
You may consider spawning off workers, maintaining a privileged master process,
|
85
|
+
and using IPC to communicate with them.
|
86
|
+
|
87
|
+
## Todo
|
88
|
+
|
89
|
+
Wrap Casper to provide DNS services, additional rights controls, etc.
|
90
|
+
|
91
|
+
|
92
|
+
## Development
|
93
|
+
|
94
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
95
|
+
`rake test` to run the tests. You can also run `bin/console` for an interactive
|
96
|
+
prompt that will allow you to experiment.
|
97
|
+
|
98
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
99
|
+
release a new version, update the version number in `version.rb`, and then run
|
100
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
101
|
+
git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
102
|
+
|
103
|
+
|
104
|
+
## Contributing
|
105
|
+
|
106
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Freaky/ruby-capsicum.
|
107
|
+
|
108
|
+
|
109
|
+
## License
|
110
|
+
|
111
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
112
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/capsicum.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'capsicum/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "capsicum"
|
8
|
+
spec.version = Capsicum::VERSION
|
9
|
+
spec.authors = ["Thomas Hurst"]
|
10
|
+
spec.email = ["tom@hur.st"]
|
11
|
+
|
12
|
+
spec.summary = %q{Experimental interface to Capsicum sandboxing}
|
13
|
+
spec.homepage = "https://github.com/Freaky/ruby-capsicum"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
20
|
+
else
|
21
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
22
|
+
"public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
f.match(%r{^(test|spec|features)/})
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
35
|
+
|
36
|
+
spec.add_runtime_dependency "ffi", "~> 1.9"
|
37
|
+
end
|
data/lib/capsicum.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require "capsicum/version"
|
2
|
+
require 'ffi'
|
3
|
+
|
4
|
+
module Capsicum
|
5
|
+
class IntPtr < FFI::Struct
|
6
|
+
layout :value, :int
|
7
|
+
end
|
8
|
+
|
9
|
+
module LibC
|
10
|
+
extend FFI::Library
|
11
|
+
ffi_lib [FFI::CURRENT_PROCESS, 'c']
|
12
|
+
|
13
|
+
attach_variable :errno, :int
|
14
|
+
|
15
|
+
attach_function :cap_enter, [], :int
|
16
|
+
attach_function :cap_getmode, [IntPtr], :int
|
17
|
+
end
|
18
|
+
|
19
|
+
# Check if we're in capability mode.
|
20
|
+
#
|
21
|
+
# @see cap_getmode(2)
|
22
|
+
#
|
23
|
+
# @return [Boolean] true if we've entered capability mode
|
24
|
+
# @raise [Errno::ENOTCAPABLE] - Capsicum not enabled.
|
25
|
+
def sandboxed?
|
26
|
+
ptr = IntPtr.new
|
27
|
+
ret = LibC.cap_getmode(ptr)
|
28
|
+
|
29
|
+
if ret == 0
|
30
|
+
ptr[:value] == 1
|
31
|
+
else
|
32
|
+
raise SystemCallError.new("cap_getmode", LibC.errno)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Enter capability sandbox mode.
|
37
|
+
#
|
38
|
+
# @see cap_enter(2)
|
39
|
+
#
|
40
|
+
# @return [Boolean] true if we've entered capability mode.
|
41
|
+
# @raise [Errno::ENOTCAPABLE] - Capsicum not enabled.
|
42
|
+
def enter!
|
43
|
+
ret = LibC.cap_enter
|
44
|
+
|
45
|
+
if ret == 0
|
46
|
+
return true
|
47
|
+
else
|
48
|
+
raise SystemCallError.new("cap_enter", LibC.errno)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Run the block within a forked process in capability mode and wait for it to
|
53
|
+
# complete.
|
54
|
+
#
|
55
|
+
# @yield block to run within the forked child.
|
56
|
+
# @return [Process::Status] exit status of the forked child.
|
57
|
+
def within_sandbox
|
58
|
+
return enum_for(:within_sandbox) unless block_given?
|
59
|
+
|
60
|
+
pid = fork do
|
61
|
+
Capsicum.enter!
|
62
|
+
yield
|
63
|
+
end
|
64
|
+
|
65
|
+
Process.waitpid2(pid).last
|
66
|
+
end
|
67
|
+
|
68
|
+
module_function :sandboxed?
|
69
|
+
module_function :enter!
|
70
|
+
module_function :within_sandbox
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: capsicum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Thomas Hurst
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: ffi
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.9'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.9'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- tom@hur.st
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- capsicum.gemspec
|
85
|
+
- lib/capsicum.rb
|
86
|
+
- lib/capsicum/version.rb
|
87
|
+
homepage: https://github.com/Freaky/ruby-capsicum
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata:
|
91
|
+
allowed_push_host: https://rubygems.org
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.6.11
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Experimental interface to Capsicum sandboxing
|
112
|
+
test_files: []
|