Skip to main content
Aotokitsuruya
Aotokitsuruya
Senior Software Developer
Published at

Kobako: From Default Allow to Deny — Balancing Convenience and Security

This article is translated by AI, if have any corrections please let me know.
This article is part of Kobako series.

From the design stage, Kobako aimed to let the Host and the Guest communicate easily, which is why the Bind service mechanism was designed in the first place.

To make it as easy as possible to use, you could pass basically any object to Sandbox#bind and call that object’s methods from the Guest. That convenience brought a huge risk, and in the end the only option was to change the design.

Escape

Most security problems come from incomplete “boundary” design. Many recent security incidents in Taiwan also started with a weak point being breached, and became severe because the attacker could keep moving to other places from there.

The Kobako Guest is, by definition, a place that “can run arbitrary code,” which makes it the weak point. And the “Bind mechanism” can reach from the Guest through to the Host, satisfying the condition for moving around, so together they form an attack path.

Under normal circumstances, we would hardly think of such a path, but once we bound an ordinary object, Ruby’s language features exposed the vulnerability.

 1class UserRepository
 2  def initialize(tenant:) = @tenant = tenant
 3  def find(email:) = "found #{email}"
 4end
 5
 6sandbox.bind("UserRepository", UserRepository.new(tenant: "acme"))
 7
 8sandbox.eval(<<~RUBY)
 9  UserRepository.send(:eval, "1 + 1")
10RUBY

We expected users to call it only as UserRepository.find(...), but we didn’t anticipate a user writing UserRepository.send(:eval, "1 + 1"). At this stage, Kobako had no idea what kind of object the user had actually bound, so it used public_send to forward the call.

1def dispatch(target, method, *args)
2  target.public_send(method, *args)
3end

As a result, UserRepository.send(...) was translated into target.public_send(:send, :eval, "1 + 1").

Even though we used #public_send, #send is still allowed to be called through #public_send, so the private method #eval, which should not be callable, was released and became something to exploit.

With #eval, you can do almost anything CRuby itself can do, and the risk goes up dramatically.

Defense

What always makes defense harder is this: if users have a legitimate reason, we shouldn’t block them, but when a malicious user exploits it, how many restrictions can we add before users find it inconvenient?

First, for the #send problem, every object gets this capability through “inheritance,” so the early version enumerated the objects carrying high-risk capabilities and refused to use those capabilities through inheritance.

1BLOCKED = [BasicObject, Kernel, Object, Module, Class]
2
3def dispatch(target, method, *args)
4  owner = target.public_method(method).owner
5  raise NoMethodError, "#{method} is not a Service method" if BLOCKED.include?(owner)
6  
7  target.public_send(method, *args)
8end

In this case, the #send method that could be exploited before now belongs to a method defined in BLOCKED, so it becomes a NoMethodError and cannot be used. That looked like enough to fix the flaw.

However, there were more boundary problems. In later security reports, we saw exploits like this.

 1class Store
 2  def initialize = @io = File.open("/etc/hostname")
 3  def backend_type = @io.class
 4end
 5
 6sandbox.bind("App::Store", Store.new)
 7
 8sandbox.eval(<<~RUBY)
 9  klass = App::Store.backend_type
10  klass.read("/etc/hostname")
11RUBY

Or exploits like this

1class UserRepositoryDecorator < SimpleDelegator; end
2
3sandbox.bind("UserRepository", UserRepositoryDecorator.new(UserRepository.new(tenant: "acme")))
4
5sandbox.eval(<<~RUBY)
6  UserRepository.method_missing(:eval, "1 + 1")
7RUBY

The first one works because #backend_type returns a Class object, which gets wrapped by the Handle mechanism. The attacker only needs to guess that the object outside is the File class, and can then escape with #read to read system files.

The second one works because SimpleDelegator defines its own #method_missing, which can still call into classes covered by BLOCKED, escaping the original BLOCKED scope once again. Even though we had designed #respond_to_guest? so users could control which operations are safe, people usually don’t set it up for convenience, so exploitable options kept appearing.

To keep these problems from happening again, Kobako now wraps every Host object by default and carefully checks each call path before letting it through. In effect, when #respond_to_guest? isn’t defined, Kobako defines a default version for everyone.

 1def exposed_methods(target)
 2  case target
 3  when Module, Delegator then []
 4  else target.class.public_instance_methods(false)
 5  end
 6end
 7
 8def dispatch(target, method, *args)
 9  allowed =
10    if target.respond_to?(:respond_to_guest?, true)
11      target.__send__(:respond_to_guest?, method)
12    else
13      exposed_methods(target).include?(method)
14    end
15  raise NoMethodError, "#{method} is not exposed to the guest" unless allowed
16
17  target.public_send(method, *args)
18end

After the change, it first checks whether #respond_to_guest? is defined to decide whether to allow the call. If not, it falls back to a generic check

  • Exclude known system objects
  • List only the object’s own public methods; private and inherited ones are not exposed

This way, unless an object itself defines a method that calls the inherited #send, these capabilities are hard to exploit again. Users do need to redefine methods like #each themselves, but this default-deny mechanism is what fully closes off this class of security problems.

Trade-off

The default-allow design made binding objects easy. Strengthening security here makes things run “a little slower” and makes the API less convenient to use.

For example, in the example that emulates Cloudflare Workers, we used to be able to bind Rack::Request directly, but now we need to rewrite it like this.

1class GuestRequest < Rack::Request
2  private
3
4  def respond_to_guest?(name) = %i[request_method path params].include?(name)
5end

Now respond_to_guest? has to be written out explicitly, but as a side effect methods like #env are blocked as well, so Host information doesn’t leak, and users have a clearer picture of what they are doing.

Even when deliberately loosened, Kobako’s current mechanism still blocks it well.

 1class Profile
 2  def initialize = @secret = "TOPSECRET"
 3
 4  private
 5
 6  def respond_to_guest?(_name) = true
 7end
 8
 9sandbox.bind("Profile", Profile.new)
10
11sandbox.eval(<<~RUBY)
12  Profile.pretty_print_instance_variables
13RUBY

In the example above, the Host has loaded the PrettyPrint library, which brings in methods like #pretty_print_instance_variables that can print @secret. Here, the check that evolved from the earlier BLOCKED list refuses any capability obtained through inheritance first, and #respond_to_guest? can only narrow the scope, never reopen it, so @secret isn’t printed by mistake.

Overall, the pursuit of the best possible developer experience made the security problem serious, and in the end the only option was to ask users for a little inconvenience and extra work, in exchange for a design they can use with confidence.