From 9341ba2ccf6e23523d8cc873eee14c22e72c58e1 Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Tue, 25 Jun 2024 00:07:00 -0300 Subject: [PATCH] Add lib/capsicum/libc.rb --- lib/capsicum.rb | 37 ++----------------------------------- lib/capsicum/libc.rb | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 35 deletions(-) create mode 100644 lib/capsicum/libc.rb diff --git a/lib/capsicum.rb b/lib/capsicum.rb index e558086..c38b4b9 100644 --- a/lib/capsicum.rb +++ b/lib/capsicum.rb @@ -4,40 +4,7 @@ require "capsicum/version" require "fiddle" module Capsicum - # @api private - module LibC - module_function - - ## - # Provides a Ruby interface for cap_enter(2) - # @return [Integer] - def cap_enter - Fiddle::Function.new( - libc["cap_enter"], - [], - Fiddle::Types::INT - ).call - end - - ## - # Provides a Ruby interface for cap_getmode(2) - # @param [Fiddle::Pointer] uintp - # @return [Integer] - def cap_getmode(uintp) - Fiddle::Function.new( - libc["cap_getmode"], - [Fiddle::Types::INTPTR_T], - Fiddle::Types::INT - ).call(uintp) - end - - ## - # @api private - def libc - @libc ||= Fiddle.dlopen Dir["/lib/libc.*"].first - end - end - + require_relative "capsicum/libc" module_function ## @@ -47,7 +14,7 @@ module Capsicum # @raise [SystemCallError] # Might raise a subclass of SystemCallError # @return [Boolean] - # Returns true if the current process is in capability mode + # Returns true when the current process is in capability mode def in_capability_mode? uintp = Fiddle::Pointer.malloc(Fiddle::SIZEOF_UINT) ret = LibC.cap_getmode(uintp) diff --git a/lib/capsicum/libc.rb b/lib/capsicum/libc.rb new file mode 100644 index 0000000..1db2faa --- /dev/null +++ b/lib/capsicum/libc.rb @@ -0,0 +1,33 @@ +# @api private +module Capsicum::LibC + module_function + + ## + # Provides a Ruby interface for cap_enter(2) + # @return [Integer] + def cap_enter + Fiddle::Function.new( + libc["cap_enter"], + [], + Fiddle::Types::INT + ).call + end + + ## + # Provides a Ruby interface for cap_getmode(2) + # @param [Fiddle::Pointer] uintp + # @return [Integer] + def cap_getmode(uintp) + Fiddle::Function.new( + libc["cap_getmode"], + [Fiddle::Types::INTPTR_T], + Fiddle::Types::INT + ).call(uintp) + end + + ## + # @api private + def libc + @libc ||= Fiddle.dlopen Dir["/lib/libc.*"].first + end +end