Class: IO

Inherits:
Object
  • Object
show all
Defined in:
lib/tcell_agent/instrumentation/monkey_patches/io.rb

Class Method Summary collapse

Class Method Details

.binread(*args, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/tcell_agent/instrumentation/monkey_patches/io.rb', line 4

def binread(*args, &block)
  path, mode = TCellAgent::Instrumentation::Lfi.extract_path_mode(*args)

  if path && TCellAgent::Instrumentation::Lfi.block_file_access?(path, mode)
    raise IOError, "tCell.io Agent: Attempted access to file #{path} with mode #{mode} denied"
  end
  cmd = TCellAgent::Cmdi.parse_command_from_open(*args)
  if cmd && TCellAgent::Cmdi.block_command?(cmd)
    raise "tCell.io Agent: Command not allowed by policy: #{cmd}"
  end

  tcell_original_binread(*args, &block)
end

.binwrite(*args, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/tcell_agent/instrumentation/monkey_patches/io.rb', line 19

def binwrite(*args, &block)
  path, _mode = TCellAgent::Instrumentation::Lfi.extract_path_mode(*args)
  mode = 'Write'

  if TCellAgent::Instrumentation::Lfi.block_file_access?(path, mode)
    raise IOError, "tCell.io Agent: Attempted access to file #{path} with mode #{mode} denied"
  end

  tcell_original_binwrite(*args, &block)
end

.foreach(*args, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/tcell_agent/instrumentation/monkey_patches/io.rb', line 31

def foreach(*args, &block)
  path, _mode = TCellAgent::Instrumentation::Lfi.extract_path_mode(*args)
  mode = 'Read'

  if TCellAgent::Instrumentation::Lfi.block_file_access?(path, mode)
    raise IOError, "tCell.io Agent: Attempted access to file #{path} with mode #{mode} denied"
  end

  tcell_original_foreach(*args, &block)
end

.popen(*args, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/tcell_agent/instrumentation/monkey_patches/io.rb', line 43

def popen(*args, &block)
  unless args.empty?
    cmd = ''

    TCellAgent::Instrumentation.safe_block('CMDI Parsing popen *args') do
      args_copy = Array.new(args)
      args_copy.shift if args_copy.first.is_a?(Hash)
      args_copy.pop if args_copy.last.is_a?(Hash)

      cmd = if args_copy.first.is_a?(String)
              args_copy.shift
            else
              TCellAgent::Cmdi.parse_command(*args_copy.shift)
            end
    end

    if TCellAgent::Cmdi.block_command?(cmd)
      raise "tCell.io Agent: Command not allowed by policy: #{cmd}"
    end
  end

  tcell_original_popen(*args, &block)
end

.read(*args, &block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/tcell_agent/instrumentation/monkey_patches/io.rb', line 68

def read(*args, &block)
  path, _mode = TCellAgent::Instrumentation::Lfi.extract_path_mode(*args)
  mode = 'Read'

  if path && TCellAgent::Instrumentation::Lfi.block_file_access?(path, mode)
    raise IOError, "tCell.io Agent: Attempted access to file #{path} with mode #{mode} denied"
  end

  cmd = TCellAgent::Cmdi.parse_command_from_open(*args)
  if cmd && TCellAgent::Cmdi.block_command?(cmd)
    raise "tCell.io Agent: Command not allowed by policy: #{cmd}"
  end
  tcell_original_read(*args, &block)
end

.readlines(*args, &block) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/tcell_agent/instrumentation/monkey_patches/io.rb', line 84

def readlines(*args, &block)
  path, _mode = TCellAgent::Instrumentation::Lfi.extract_path_mode(*args)
  mode = 'Read'

  if path && TCellAgent::Instrumentation::Lfi.block_file_access?(path, mode)
    raise IOError, "tCell.io Agent: Attempted access to file #{path} with mode #{mode} denied"
  end

  cmd = TCellAgent::Cmdi.parse_command_from_open(*args)
  if cmd && TCellAgent::Cmdi.block_command?(cmd)
    raise "tCell.io Agent: Command not allowed by policy: #{cmd}"
  end

  tcell_original_readlines(*args, &block)
end

.sysopen(*args, &block) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/tcell_agent/instrumentation/monkey_patches/io.rb', line 101

def sysopen(*args, &block)
  path, mode = TCellAgent::Instrumentation::Lfi.extract_path_mode(*args)

  if TCellAgent::Instrumentation::Lfi.block_file_access?(path, mode)
    raise IOError, "tCell.io Agent: Attempted access to file #{path} with mode #{mode} denied"
  end

  tcell_original_sysopen(*args, &block)
end

.tcell_original_binreadObject



3
# File 'lib/tcell_agent/instrumentation/monkey_patches/io.rb', line 3

alias_method :tcell_original_binread, :binread

.tcell_original_binwriteObject



18
# File 'lib/tcell_agent/instrumentation/monkey_patches/io.rb', line 18

alias_method :tcell_original_binwrite, :binwrite

.tcell_original_foreachObject



30
# File 'lib/tcell_agent/instrumentation/monkey_patches/io.rb', line 30

alias_method :tcell_original_foreach, :foreach

.tcell_original_popenObject



42
# File 'lib/tcell_agent/instrumentation/monkey_patches/io.rb', line 42

alias_method :tcell_original_popen, :popen

.tcell_original_readObject



67
# File 'lib/tcell_agent/instrumentation/monkey_patches/io.rb', line 67

alias_method :tcell_original_read, :read

.tcell_original_readlinesObject



83
# File 'lib/tcell_agent/instrumentation/monkey_patches/io.rb', line 83

alias_method :tcell_original_readlines, :readlines

.tcell_original_sysopenObject



100
# File 'lib/tcell_agent/instrumentation/monkey_patches/io.rb', line 100

alias_method :tcell_original_sysopen, :sysopen

.tcell_original_writeObject



111
# File 'lib/tcell_agent/instrumentation/monkey_patches/io.rb', line 111

alias_method :tcell_original_write, :write

.write(*args, &block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/tcell_agent/instrumentation/monkey_patches/io.rb', line 112

def write(*args, &block)
  path, _mode = TCellAgent::Instrumentation::Lfi.extract_path_mode(*args)
  mode = 'Write'

  if TCellAgent::Instrumentation::Lfi.block_file_access?(path, mode)
    raise IOError, "tCell.io Agent: Attempted access to file #{path} with mode #{mode} denied"
  end

  tcell_original_write(*args, &block)
end