capng_c 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2bb0e5ea482272f6f1678202e013e2279429a8d8db9331560a0037d5d3affee5
4
- data.tar.gz: d00913ec86725ff56ec782cec00ade00c12ce7583963dabe080d2ae7f43a6de2
3
+ metadata.gz: 1eaea356dec8a2e8049a45af2c57d5839e661f64cfcaa2379cb5f27e69ab6ff4
4
+ data.tar.gz: a900c6d4381d353872be092de3e3be68ae311c6c0a43a534da4800eb89b3bada
5
5
  SHA512:
6
- metadata.gz: 7fffbe03cb38e5237a24a4dacd9c0b0908d9d63692f04ef2c8e4e0181d96860f3ca0c6d2a7c35319123aaf6176b1faca303c8a6d4ddff1cd7c413250297112b6
7
- data.tar.gz: 022ef6afb55bcaac0b83e8a31ae634f7839a2f8a6f49dc493328f0dd8ee44f04420227cf37eee3169142d6b5bbe8ee1c5df976f4e0158deffa225545c6c863c9
6
+ metadata.gz: de48892bc4462b41a9d47eeccaf1397d62c3e03c8e6138600bec281ab4bf2b34b3d8352449bf52b2fe2fe7cbdd1717203d27d5d623e1e3cd3805bc119d031119
7
+ data.tar.gz: 6f4310683b133bcfb366366c670b68f8ad24eb6eb9065eb3c09a9d8b66e32106eeb38ac508dfd13a8d680d00ed74ba17618c25127f0dd0ecf0db199e2b0371d3
@@ -0,0 +1,36 @@
1
+ # Copyright 2020- Hiroshi Hatake
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'capng'
16
+
17
+ if ARGV.size != 1
18
+ puts "specify file path on ARGV."
19
+ exit 1
20
+ end
21
+
22
+ if Process.uid != 0
23
+ puts "Needed to run as root!"
24
+ exit 2
25
+ end
26
+
27
+ path = ARGV[0]
28
+ capng = CapNG.new(:file, path)
29
+ print = CapNG::Print.new
30
+ puts "capability: #{print.caps_text(:buffer, :effective)}"
31
+ capng.clear(:caps)
32
+ ret = capng.update(:add, CapNG::Type::EFFECTIVE | CapNG::Type::INHERITABLE | CapNG::Type::PERMITTED,
33
+ [:dac_read_search, :dac_override])
34
+ puts "updating capability: #{ret ? "success" : "fail"}"
35
+ capng.apply_caps_file(path)
36
+ puts "updated capability: #{print.caps_text(:buffer, :effective)}"
@@ -0,0 +1,59 @@
1
+ # Copyright 2020- Hiroshi Hatake
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'capng'
16
+
17
+ if Process.uid != 0
18
+ puts "Needed to run as root!"
19
+ exit 2
20
+ end
21
+
22
+ capng = CapNG.new(:current_process)
23
+
24
+ print = CapNG::Print.new
25
+ puts "capability: #{print.caps_text(:buffer, :effective)}"
26
+ target_file = ARGV[0] || "/var/log/syslog"
27
+ capng.clear(:caps)
28
+
29
+ puts "capability: #{print.caps_text(:buffer, :effective)}"
30
+ ret = capng.update(:add, CapNG::Type::EFFECTIVE | CapNG::Type::INHERITABLE | CapNG::Type::PERMITTED, :dac_read_search)
31
+ puts "CapNG#update: #{ret ? 'success' : 'fail'}"
32
+
33
+ ret = capng.apply(:caps)
34
+ puts "CapNG#apply(add): #{ret ? 'success' : 'fail'}"
35
+ puts "capability: #{print.caps_text(:buffer, :effective)}"
36
+ path = "/var/log/syslog"
37
+ unless File.readable?(path)
38
+ puts "-----unreadable!!!!-----\ntarget: #{target_file}"
39
+ end
40
+ contents = File.read(target_file)
41
+ if contents.length >= 0
42
+ puts "succeeded to read: #{target_file}"
43
+ end
44
+
45
+ ret = capng.update(:drop, CapNG::Type::EFFECTIVE | CapNG::Type::INHERITABLE | CapNG::Type::PERMITTED, :dac_read_search)
46
+ puts "CapNG#update(drop): #{ret ? 'success' : 'fail'}"
47
+ puts "capability: #{print.caps_text(:buffer, :effective)}"
48
+
49
+ ret = capng.apply(:caps)
50
+ puts "CapNG#apply(drop): #{ret ? 'success' : 'fail'}"
51
+
52
+ unless File.readable?(path)
53
+ puts "-----unreadable!!!!-----\ntarget: #{target_file}"
54
+ end
55
+ begin
56
+ File.read(target_file)
57
+ rescue Errno::EACCES
58
+ puts "permission denied even if run as root"
59
+ end
@@ -88,14 +88,20 @@ rb_capng_initialize(int argc, VALUE *argv, VALUE self)
88
88
  fptr = RFILE(rb_pid_or_file)->fptr;
89
89
  fd = fptr->fd;
90
90
  result = capng_get_caps_fd(fd);
91
- if (result != 0) {
92
- rb_raise(rb_eRuntimeError, "Couldn't get current file capability");
93
- }
91
+ /* Just store result into instance variable. */
92
+ /* This is because capng_get_caps_fd should return 0 if file cap is not set. */
93
+ rb_iv_set(self, "@return_code", INT2NUM(result));
94
94
  }
95
95
 
96
96
  return Qnil;
97
97
  }
98
98
 
99
+ static VALUE
100
+ rb_capng_return_code(VALUE self)
101
+ {
102
+ return rb_iv_get(self, "@return_code");
103
+ }
104
+
99
105
  static VALUE
100
106
  rb_capng_clear(VALUE self, VALUE rb_select_name_or_enum)
101
107
  {
@@ -398,6 +404,7 @@ Init_capng(void)
398
404
  rb_define_alloc_func(rb_cCapNG, rb_capng_alloc);
399
405
 
400
406
  rb_define_method(rb_cCapNG, "initialize", rb_capng_initialize, -1);
407
+ rb_define_method(rb_cCapNG, "return_code", rb_capng_return_code, 0);
401
408
  rb_define_method(rb_cCapNG, "clear", rb_capng_clear, 1);
402
409
  rb_define_method(rb_cCapNG, "fill", rb_capng_fill, 1);
403
410
  rb_define_method(rb_cCapNG, "setpid", rb_capng_setpid, 1);
@@ -10,16 +10,14 @@ class CapNG
10
10
  alias_method :initialize_raw, :initialize
11
11
 
12
12
  def initialize(target = nil, pid_or_path = nil)
13
- if target.nil?
14
- initialize_raw
15
- elsif target && pid_or_path.is_a?(Integer)
13
+ if target && pid_or_path.is_a?(Integer)
16
14
  initialize_raw(target, pid_or_path)
17
15
  elsif target && pid_or_path.is_a?(String) && File.exist?(pid_or_path)
18
- File.open(pid_or_path) do
19
- initialize_raw(target, pid_or_path);
16
+ File.open(pid_or_path) do |file|
17
+ initialize_raw(target, file);
20
18
  end
21
19
  else
22
- initialize_raw
20
+ initialize_raw(target, pid_or_path)
23
21
  end
24
22
  end
25
23
 
@@ -38,7 +36,7 @@ class CapNG
38
36
  def apply_caps_file(file_or_string_path)
39
37
  if file_or_string_path.is_a?(String) && File.exist?(file_or_string_path)
40
38
  File.open(file_or_string_path) do |f|
41
- apply_cps_file_raw(f)
39
+ apply_caps_file_raw(f)
42
40
  end
43
41
  elsif file_or_string_path.is_a?(File)
44
42
  apply_caps_file_raw(file_or_string_path)
@@ -1,3 +1,3 @@
1
1
  class CapNG
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capng_c
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroshi Hatake
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-28 00:00:00.000000000 Z
11
+ date: 2020-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -89,6 +89,8 @@ files:
89
89
  - bin/console
90
90
  - bin/setup
91
91
  - capng_c.gemspec
92
+ - example/file_capability.rb
93
+ - example/process_capability.rb
92
94
  - ext/capng/capability.c
93
95
  - ext/capng/capng.c
94
96
  - ext/capng/capng.h
@@ -120,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
122
  - !ruby/object:Gem::Version
121
123
  version: '0'
122
124
  requirements: []
123
- rubygems_version: 3.1.4
125
+ rubygems_version: 3.0.3
124
126
  signing_key:
125
127
  specification_version: 4
126
128
  summary: libcap-ng bindings for Ruby.