haconiwa 0.0.1.pre2 → 0.0.1.pre3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5118b20649292936469ac7f8c20186aba2b43609
4
- data.tar.gz: ce1583a83126f75f922bc83d40098df36afb6340
3
+ metadata.gz: 1977a089936273a9c144d238d24a65371b8b5f97
4
+ data.tar.gz: f08c51b80e7450b969ee8352faacf5d85d69ee2a
5
5
  SHA512:
6
- metadata.gz: 423cff4515ed3116b3ca8f317cc8d7cfaf392c77a6725948041edb35b92b4e24fd32da208d1b73b885178e971eeeb24f01da871ddeabc541ffc5377fb83ea8c6
7
- data.tar.gz: 3ce74b9dde1970f2a8b87fe27f4f0d988f83620c968010e98e1b6dffced9d2436e91b3397fadf8f602c6cf9b8ec4fe591d98ad32c91cb799bf300a352ad2f788
6
+ metadata.gz: 6c2131693b7224ad6beba0ff59bbbc131cb3a5068552d812e06372ba1aa48134d0e32c721fe0ea32d22a1ce805cae8a9d80ca2076287ccacdbcbffb708a8c05e
7
+ data.tar.gz: 107f85c23c0d32e17c876ea2e892f26bdf20b15a6a9899cf68cae0032c76f1d143033c7be36f6b8c7ea872268ed168e4a30716e5e1ad2d983be997f746535920
data/bin/console CHANGED
@@ -7,8 +7,5 @@ require "haconiwa"
7
7
  # with your gem easier. You can also use a different console, if you like.
8
8
 
9
9
  # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start
10
+ require "pry"
11
+ Pry.start
@@ -0,0 +1,25 @@
1
+ require 'haconiwa'
2
+ require 'pathname'
3
+ haconiwa = Haconiwa::Base.define do |config|
4
+ config.name = "drop-time001" # to be hostname
5
+
6
+ root = Pathname.new("/var/haconiwa/root")
7
+ config.add_mount_point "/var/haconiwa/rootfs", to: root, readonly: true
8
+ config.add_mount_point "/lib64", to: root.join("lib64"), readonly: true
9
+ config.add_mount_point "/sbin", to: root.join("sbin"), readonly: true
10
+ config.add_mount_point "/usr/bin", to: root.join("usr/bin"), readonly: true
11
+ config.add_mount_point "/usr/local/rbenv", to: root.join("usr/local/rbenv")
12
+ config.add_mount_point "tmpfs", to: root.join("tmp"), fs: "tmpfs"
13
+ config.mount_independent_procfs
14
+ config.chroot_to root
15
+
16
+ config.namespace.unshare "mount"
17
+ config.namespace.unshare "ipc"
18
+ config.namespace.unshare "uts"
19
+ config.namespace.unshare "pid"
20
+
21
+ config.capabilities.allow :all
22
+ config.capabilities.drop "cap_sys_time"
23
+ end
24
+
25
+ haconiwa.start("/bin/bash")
@@ -1,3 +1,5 @@
1
+ require 'haconiwa/small_libcap'
2
+
1
3
  module Haconiwa
2
4
  class Capabilities
3
5
  def initialize
@@ -8,11 +10,28 @@ module Haconiwa
8
10
  def allow(*keys)
9
11
  if keys.first == :all
10
12
  @whitelist.clear
13
+ else
14
+ @whitelist.concat(keys)
11
15
  end
12
16
  end
13
17
 
14
18
  def drop(*keys)
15
19
  @blacklist.concat(keys)
16
20
  end
21
+
22
+ def apply!
23
+ if acts_as_whitelist?
24
+ SmallLibcap.apply_cap_whitelist(list: @whitelist.uniq)
25
+ else
26
+ @blacklist.uniq.each do |n|
27
+ SmallLibcap.drop_cap_by_name(n)
28
+ end
29
+ end
30
+ end
31
+
32
+ private
33
+ def acts_as_whitelist?
34
+ ! @whitelist.empty?
35
+ end
17
36
  end
18
37
  end
@@ -28,6 +28,8 @@ module Haconiwa::Runners
28
28
  wrapper.close
29
29
  FileUtils.chmod 0700, wrapper.path
30
30
 
31
+ base.capabilities.apply!
32
+
31
33
  if base.namespace.use_pid_ns
32
34
  Bundler.with_clean_env {
33
35
  exec "unshare", "--pid", "--", wrapper.path, init_command
@@ -0,0 +1,62 @@
1
+ require 'ffi'
2
+
3
+ module Haconiwa
4
+ class Cap_T < FFI::ManagedStruct
5
+ layout :head, :pointer,
6
+ :set, :pointer
7
+
8
+ def self.release(ptr)
9
+ SmallLibcap.cap_free ptr
10
+ end
11
+ end
12
+
13
+ class SmallLibcap
14
+ class CapError < StandardError; end
15
+
16
+ extend FFI::Library
17
+ ffi_lib "libcap.so.2"
18
+
19
+ attach_function :cap_get_proc, [], Cap_T.ptr
20
+ attach_function :cap_set_proc, [Cap_T.ptr], :int
21
+ attach_function :cap_from_name, [:string, :pointer], :int
22
+ attach_function :cap_drop_bound, [:int], :int
23
+ attach_function :cap_get_bound, [:int], :int
24
+
25
+ attach_function :cap_free, [:pointer], :int
26
+
27
+ def self.cap_supported?(cap)
28
+ cap_get_bound(cap) >= 0
29
+ end
30
+
31
+ def self._name2cap(name)
32
+ ptr = FFI::MemoryPointer.new(:int)
33
+ err = cap_from_name(name, ptr)
34
+ if err < 0
35
+ raise CapError, "Invalid or unsupported capability name: #{name}"
36
+ end
37
+ ptr.read_int
38
+ end
39
+
40
+ def self.drop_cap_by_name(name)
41
+ err = cap_drop_bound(_name2cap(name))
42
+ if err < 0
43
+ raise CapError, "Failed to drop capability name: #{name} from bounding set"
44
+ end
45
+ true
46
+ end
47
+
48
+ def self.apply_cap_whitelist(list: [])
49
+ whitelist = list.map{|n| _name2cap(n) }
50
+
51
+ loop.with_index(0) do |_, cap_value|
52
+ return(true) unless cap_supported?(cap_value)
53
+ next if whitelist.include?(cap_value)
54
+
55
+ err = cap_drop_bound(cap_value)
56
+ if err < 0
57
+ raise CapError, "Failed to drop capability cap_value_t: #{cap_value} from bounding set"
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -1,3 +1,3 @@
1
1
  module Haconiwa
2
- VERSION = "0.0.1.pre2"
2
+ VERSION = "0.0.1.pre3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haconiwa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.pre2
4
+ version: 0.0.1.pre3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uchio KONDO
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-31 00:00:00.000000000 Z
11
+ date: 2016-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -128,6 +128,7 @@ files:
128
128
  - bin/setup
129
129
  - examples/chroot.rb
130
130
  - examples/cpu.rb
131
+ - examples/drop_cap_sys_time.rb
131
132
  - haconiwa.gemspec
132
133
  - lib/haconiwa.rb
133
134
  - lib/haconiwa/base.rb
@@ -139,6 +140,7 @@ files:
139
140
  - lib/haconiwa/runners.rb
140
141
  - lib/haconiwa/runners/linux.rb
141
142
  - lib/haconiwa/small_cgroup.rb
143
+ - lib/haconiwa/small_libcap.rb
142
144
  - lib/haconiwa/version.rb
143
145
  homepage: https://github.com/udzura/haconiwa
144
146
  licenses: