capsicum 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in capsicum.gemspec
4
+ gemspec
@@ -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.
@@ -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
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "capsicum"
5
+
6
+ require "irb"
7
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -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
@@ -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
@@ -0,0 +1,3 @@
1
+ module Capsicum
2
+ VERSION = "0.1.0"
3
+ 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: []