memory_locker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1f3a6f1d81fcec94948578a19104e6465844402d3a58c2379b8c9a02e973fe8a
4
+ data.tar.gz: 0c5133932a48f4409961bc2fc6beeb53c28b183f754497166ff401eabecc7c03
5
+ SHA512:
6
+ metadata.gz: 8f6c0e2f07e918f1971f7d0c7f1ddd5c8f82c42e438e796b5c829e46856371dbeaf7716c59f784541b7ee2f43e59648088ee7f65c4195d3df884fb192fe79f94
7
+ data.tar.gz: 76494e83446a77fdc49b45c4c6ba51a59a1aafb64f238052c053b3bfcf8c80cb025a7dfddaa0bc9a1513a7730b2c2b13a5c371877dee53d2680fc406325f474a
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.1.0] - 2023-07-27
2
+
3
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Paweł Pokrywka
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # MemoryLocker
2
+
3
+ Lock memory containing sensitive data (such as passwords or cryptographic keys) to prevent it from being swapped
4
+ by the kernel, which allows the attacker with access to swap space to recover secrets.
5
+
6
+ Ruby doesn't allow granular memory management, therefore the approach is to lock the entire memory of a program.
7
+
8
+ ## Requirements
9
+
10
+ This gem requires `ffi` gem, which needs to be built on install.
11
+ In case of build-related issues, make sure you have the compiler installed.
12
+
13
+ In Debian-based Linux distributions, you can install it by executing:
14
+
15
+ $ sudo apt install --no-install-recommends build-essential
16
+
17
+ Refer to [ffi gem documentation](https://github.com/ffi/ffi) for requirements on other systems.
18
+
19
+ ## Installation
20
+
21
+ Install the gem and add it to the application's Gemfile by executing:
22
+
23
+ $ bundle add memory_locker
24
+
25
+ If the bundler is not being used to manage dependencies, install the gem by executing:
26
+
27
+ $ gem install memory_locker
28
+
29
+ ## Usage
30
+
31
+ To lock the memory of the current process use the following once, whenever you want,
32
+ but before sensitive data processing:
33
+
34
+ MemoryLocker.new(:glibc).lock!
35
+
36
+ The above example uses the `glibc` backend which should work on most Linux distributions.
37
+ Currently, only this backend is implemented, however, it is trivial to add support for other c-libraries.
38
+
39
+ The memory will stay locked until the process terminates. There is no way to unlock memory.
40
+ The reason is Ruby doesn't support reliable removal of secrets from memory, therefore it is safer to just
41
+ keep memory locked.
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `rake spec` to run the tests.
46
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
47
+
48
+ To implement the backend for a different c-library, use existing one as a template.
49
+ Copy `lib/memory_locker/glibc.rb` to `lib/memory_locker/my_backend.rb`, and change it.
50
+ Later use `:my_backend` as an argument to `MemoryLocker` initializer.
51
+
52
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
53
+ version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
54
+ push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/phantom-node/memory_locker.
59
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
60
+ the [code of conduct](https://github.com/phantom-node/memory_locker/blob/master/CODE_OF_CONDUCT.md).
61
+
62
+ ## License
63
+
64
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
65
+
66
+ ## Code of Conduct
67
+
68
+ Everyone interacting in the MemoryLocker project's codebases, issue trackers, chat rooms, and mailing lists
69
+ is expected to follow the [code of conduct](https://github.com/phantom-node/memory_locker/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MemoryLocker
4
+ # Low level interface to glibc
5
+ module Backend
6
+ extend FFI::Library
7
+ ffi_lib 'libc.so.6'
8
+
9
+ MCL_CURRENT = 1
10
+ MCL_FUTURE = 2
11
+
12
+ attach_function :mlockall, [:int], :int
13
+
14
+ def self.lock!
15
+ mlockall(MCL_CURRENT | MCL_FUTURE).zero?
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MemoryLocker
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'memory_locker/version'
4
+ require 'ffi'
5
+
6
+ # Lock process memory, so it won't be swapped by the kernel.
7
+ # It is implemented as a one-way operation: there is no unlock.
8
+ # That's because it's hard to properly clean memory in Ruby.
9
+ class MemoryLocker
10
+ LockingError = Class.new StandardError
11
+
12
+ def lock!
13
+ Backend.lock! || raise(LockingError, "Failed to lock memory, errno #{FFI.errno}")
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :backend
19
+
20
+ def initialize(backend)
21
+ require_relative "memory_locker/#{backend}"
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: memory_locker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paweł Pokrywka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-07-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0
27
+ description:
28
+ email:
29
+ - pepawel@users.noreply.github.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - LICENSE.txt
36
+ - README.md
37
+ - lib/memory_locker.rb
38
+ - lib/memory_locker/glibc.rb
39
+ - lib/memory_locker/version.rb
40
+ homepage: https://github.com/phantom-node/memory_locker
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ homepage_uri: https://github.com/phantom-node/memory_locker
45
+ source_code_uri: https://github.com/phantom-node/memory_locker
46
+ changelog_uri: https://github.com/phantom-node/memory_locker/blob/master/CHANGELOG.md
47
+ rubygems_mfa_required: 'true'
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.7.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.1.6
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Lock memory containing sensitive data to prevent it from being swapped by
67
+ the kernel
68
+ test_files: []