diff --git a/.bundle/config b/.bundle/config new file mode 100644 index 0000000..d747edd --- /dev/null +++ b/.bundle/config @@ -0,0 +1,2 @@ +--- +BUNDLE_PATH: ".gems" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8436db2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.gems/ +/tmp/ +*.so diff --git a/.projectile b/.projectile new file mode 100644 index 0000000..3f0d314 --- /dev/null +++ b/.projectile @@ -0,0 +1,2 @@ +-/.gems/ +-/tmp/ diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..3be9c3c --- /dev/null +++ b/Gemfile @@ -0,0 +1,2 @@ +source "https://rubygems.org" +gemspec diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..6663880 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,22 @@ +PATH + remote: . + specs: + hbsdctl.rb (0.1.0) + +GEM + remote: https://rubygems.org/ + specs: + rake (13.1.0) + rake-compiler (1.2.0) + rake + +PLATFORMS + amd64-freebsd-14 + ruby + +DEPENDENCIES + hbsdctl.rb! + rake-compiler (= 1.2.0) + +BUNDLED WITH + 2.5.3 diff --git a/README.md b/README.md index 142b566..12323c8 100644 --- a/README.md +++ b/README.md @@ -1,93 +1,45 @@ -# hbsdctl.rb +## About +hbsdctl.rb is a Ruby C extension that binds libhbsdcontrol from the +[hardenedbsd](https://hardenedbsd.org) project. Through this library, +you can query what features are available and if root, enable or disable +those features for given binaries. +## Examples -## Getting started +__Available features__ -To make it easy for you to get started with GitLab, here's a list of recommended next steps. +As a regular user account, you can obtain a list of available features. +But to enable or disable those features a superuser account is required: -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: - -``` -cd existing_repo -git remote add origin https://git.hardenedbsd.org/0x1eef/hbsdctl.rb.git -git branch -M main -git push -uf origin main +``` ruby +#!/usr/bin/env ruby +# As a regular user account +require 'hbsdctl' +BSD::Control + .available_features + .each do + print "The ", _1.name, " feature is available", "\n" +end ``` -## Integrate with your tools +__Enable feature__ -- [ ] [Set up project integrations](https://git.hardenedbsd.org/0x1eef/hbsdctl.rb/-/settings/integrations) +As a superuser account, you can enable or disable features for a given executable. +The example enables the mprotect feature for the emacs binary: -## Collaborate with your team - -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) - -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** - -# Editing this README - -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README - -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. - -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. - -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. - -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. - -## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. - -## Contributing -State if you are open to contributions and what your requirements are for accepting them. - -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. - -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. - -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. +``` ruby +#!/usr/bin/env ruby +# As a root account +require 'hbsdctl' +BSD::Control + .feature!("mprotect") + .enable!("/usr/local/bin/emacs") +``` ## License -For open source projects, say how it is licensed. -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. +[BSD Zero Clause](https://choosealicense.com/licenses/0bsd/). +
+See [LICENSE](./LICENSE). + diff --git a/Rakefile.rb b/Rakefile.rb new file mode 100644 index 0000000..8b4ca21 --- /dev/null +++ b/Rakefile.rb @@ -0,0 +1,4 @@ +require "bundler/setup" +require "rake/extensiontask" +Rake::ExtensionTask.new("hbsdctl.rb") +task default: %w[clobber compile test] diff --git a/ext/hbsdctl.rb/extconf.rb b/ext/hbsdctl.rb/extconf.rb new file mode 100644 index 0000000..f9c2f74 --- /dev/null +++ b/ext/hbsdctl.rb/extconf.rb @@ -0,0 +1,3 @@ +require 'mkmf' +$LIBS << ' -lsbuf -lhbsdcontrol' +create_makefile("hbsdctl.rb") diff --git a/ext/hbsdctl.rb/hbsdctl.c b/ext/hbsdctl.rb/hbsdctl.c new file mode 100644 index 0000000..ec93208 --- /dev/null +++ b/ext/hbsdctl.rb/hbsdctl.c @@ -0,0 +1,63 @@ +#include +#include + +static VALUE +ffi_available_features(VALUE self) +{ + const struct pax_feature_entry *entry = &pax_features[0]; + VALUE rb_mBSD = rb_const_get(rb_cObject, rb_intern("BSD")), + rb_mControl = rb_const_get(rb_mBSD, rb_intern("Control")), + rb_cFeature = rb_const_get(rb_mControl, rb_intern("Feature")), + features = rb_ary_new(), + feature = 0; + + while (entry->feature != NULL) { + feature = rb_funcall( + rb_cFeature, + rb_intern("new"), + 3, + rb_str_new2(entry->feature), + rb_str_new2(entry->extattr[1]), + rb_str_new2(entry->extattr[0]) + ); + rb_ary_push(features, feature); + entry++; + } + return features; +} + +static VALUE +feature_set(VALUE self, VALUE path, VALUE state) +{ + int r; + char *cpath; + + Check_Type(path, T_STRING); + Check_Type(state, T_FIXNUM); + cpath = RSTRING_PTR(path); + r = hbsdcontrol_set_feature_state( + cpath, + RSTRING_PTR(rb_funcall(self, rb_intern("name"), 0)), + NUM2INT(state) + ); + if (r == 0) { + return Qtrue; + } else { + return Qfalse; + } +} + +void +Init_hbsdctl(void) +{ + VALUE rb_mBSD = rb_const_get(rb_cObject, rb_intern("BSD")), + rb_mControl = rb_const_get(rb_mBSD, rb_intern("Control")), + rb_cFeature = rb_const_get(rb_mControl, rb_intern("Feature")), + rb_mFFI = rb_const_get(rb_mControl, rb_intern("FFI")); + + rb_define_const(rb_mBSD, "SysDef", -1); + rb_define_const(rb_mBSD, "Disable", 0); + rb_define_const(rb_mBSD, "Enable", 1); + rb_define_singleton_method(rb_mFFI, "available_features", ffi_available_features, 0); + rb_define_method(rb_cFeature, "set!", feature_set, 2); +} diff --git a/hbsdctl.rb.gemspec b/hbsdctl.rb.gemspec new file mode 100644 index 0000000..b41e983 --- /dev/null +++ b/hbsdctl.rb.gemspec @@ -0,0 +1,14 @@ +Gem::Specification.new do |gem| + gem.name = "hbsdctl.rb" + gem.authors = ["0x1eef"] + gem.email = ["0x1eef@protonmail.com"] + gem.homepage = "https://github.com/0x1eef/hbsdctl.rb#readme" + gem.version = "0.1.0" + gem.licenses = ["0BSD"] + gem.files = `git ls-files`.split($/) + gem.require_paths = ["lib"] + gem.extensions = %w[ext/hbsdctl.rb/extconf.rb] + gem.summary = "A Ruby interface for libhbsdcontrol" + gem.description = gem.summary + gem.add_development_dependency "rake-compiler", "= 1.2.0" +end diff --git a/lib/bsd/control.rb b/lib/bsd/control.rb new file mode 100644 index 0000000..1566d83 --- /dev/null +++ b/lib/bsd/control.rb @@ -0,0 +1,32 @@ +module BSD::Control + require_relative "control/feature" + Error = Class.new(RuntimeError) + + ## + # @return [Array] + def self.available_features + Feature.available + end + + ## + # @example + # BSD::Control + # .feature!("mprotect") + # .set!("/usr/local/bin/firefox", BSD::Enable) + # + # @param [String] name + # The name of a feature. + # + # @raise [BSD::Control::Error] + # When a feature is not found. + # + # @return [BSD::Control::Feature] + # Returns an instance of BSD::Control::Feature. + def self.feature!(name) + feature = Feature.available.find { _1.name == name } + feature ? feature : raise(Error, "feature '#{name}' wasn't found") + end + + module FFI + end +end diff --git a/lib/bsd/control/feature.rb b/lib/bsd/control/feature.rb new file mode 100644 index 0000000..8185920 --- /dev/null +++ b/lib/bsd/control/feature.rb @@ -0,0 +1,102 @@ +module BSD::Control + class Feature < Struct.new(:name, :enable, :disable) + ## + # @return [Array] + # Returns an array of available features. + def self.available + BSD::Control::FFI.available_features + end + + ## + # Enables a feature for a given binary. + # + # @param [String] path + # The path to a binary. + # + # @return [Boolean] + # Returns true on success. + def enable!(path) + set!(path, BSD::Control::Enable) + end + + ## + # Disables a feature for a given binary. + # + # @param [String] path + # The path to a binary. + # + # @return [Boolean] + # Returns true on success. + def disable!(path) + set!(path, BSD::Control::Disable) + end + + ## + # @group Predicates + + ## + # @return [Boolean] + # Returns true for the pageexec feature. + def pageexec? + name == "pageexec" + end + + ## + # @return [Boolean] + # Returns true for the mprotect feature. + def mprotect? + name == "mprotect" + end + + ## + # @return [Boolean] + # Returns true for the segv-guard feature. + def segvguard? + name == "segvguard" + end + + ## + # @return [Boolean] + # Returns true for the ASLR feature. + def aslr? + name == "aslr" + end + + ## + # @return [Boolean] + # Returns true for the shlibrandom feature. + def shlibrandom? + name == "shlibrandom" + end + + ## + # @return [Boolean] + # Returns true for the disallow-map32bit feature. + def disallow_map32bit? + name == "disallow_map32bit" + end + + ## + # @return [Boolean] + # Returns true for the insecure kmod feature. + def insecure_kmod? + name == "insecure_kmod" + end + + ## + # @return [Boolean] + # Returns true for the harden SHM feature. + def harden_shm? + name == "harden_shm" + end + + ## + # @return [Boolean] + # Returns true for the prohibit ptrace capsicum feature. + def prohibit_ptrace_capsicum? + name == "prohibit_ptrace_capsicum" + end + + # @endgroup + end +end diff --git a/lib/hbsdctl.rb b/lib/hbsdctl.rb new file mode 100644 index 0000000..6cbe3e1 --- /dev/null +++ b/lib/hbsdctl.rb @@ -0,0 +1,4 @@ +module BSD + require_relative "bsd/control" + require_relative "hbsdctl.rb.so" +end