Skip to content

security: remove unnecessary calls to os.ExpandEnv#81298

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
knz:20220516-env
May 16, 2022
Merged

security: remove unnecessary calls to os.ExpandEnv#81298
craig[bot] merged 1 commit intocockroachdb:masterfrom
knz:20220516-env

Conversation

@knz
Copy link
Copy Markdown
Contributor

@knz knz commented May 16, 2022

Fixes #69412.

Previously to this patch, the code in the pkg/security package
interpreted the --certs-dir command-line flag using the Go function
os.ExpandEnv.

This mades it possible to indirectly use environment variables when
specifying the value of --certs-dir. For example, the user could type
--certs-dir='$HOME/.certs'. Note the single quotes: the expansion is
performed inside the process (by Go), not by the shell.

There are several problems with this approach:

  • it is surprising -- this type of expansion is extremely uncommon for
    the command-line flags of server services, and generally only used
    when parsing configuration files. The reason why it is uncommon is
    that there is already another mechanism that can do this, namely the
    shell's own expansion facilities, and therefore this double expansion
    is not really needed.

  • it makes it difficult/impossible to use in the (albeit unusual) case
    where the directory name on disk contains a dollar sign.

  • it may constitute a security vulnerability, as it enables the flag
    expansion to access arbitrary environment variables in the shell that
    launches crdb and observe their value by looking at the error messages
    upon certificate loads. For example, a malicious user that can
    indirectly control the flag can specify
    --certs-dir=$COCKROACH_LICENSE_KEY and discover the env var value in
    the error message even if they don't have access to env vars
    otherwise.

Finally, this functionality is probably never used. We do not mention
it in docs, and experience reading deployment scripts suggests it is
not being used in practice.

Release note (backward-incompatible change): CockroachDB does not
perform env var expansion in the parameter --certs-dir anymore. This
was an undocumented feature. Uses like
--certs-dir='$HOME/path' (expansion by CockroachDB) can be
replaced by --certs-dir="$HOME/path" (expansion by the unix shell).

Previously to this patch, the code in the pkg/security package
interpreted the `--certs-dir` command-line flag using the Go function
`os.ExpandEnv`.

This mades it possible to indirectly use environment variables when
specifying the value of `--certs-dir`. For example, the user could type
`--certs-dir='$HOME/.certs'`. Note the single quotes: the expansion is
performed inside the process (by Go), not by the shell.

There are several problems with this approach:

- it is surprising -- this type of expansion is extremely uncommon for
  the command-line flags of server services, and generally only used
  when parsing configuration files. The reason why it is uncommon is
  that there is already another mechanism that can do this, namely the
  shell's own expansion facilities, and therefore this double expansion
  is not really needed.

- it makes it difficult/impossible to use in the (albeit unusual) case
  where the directory name on disk contains a dollar sign.

- it may constitute a security vulnerability, as it enables the flag
  expansion to access arbitrary environment variables in the shell that
  launches crdb and observe their value by looking at the error messages
  upon certificate loads. For example, a malicious user that can
  indirectly control the flag can specify
  `--certs-dir=$COCKROACH_LICENSE_KEY` and discover the env var value in
  the error message even if they don't have access to env vars
  otherwise.

Finally, this functionality is probably never used. We do not mention
it in docs, and experience reading deployment scripts suggests it is
not being used in practice.

Release note (backward-incompatible change): CockroachDB does not
perform env var expansion in the parameter `--certs-dir` anymore. This
was an undocumented feature.  Uses like
`--certs-dir='$HOME/path'` (expansion by CockroachDB) can be
replaced by `--certs-dir="$HOME/path"` (expansion by the unix shell).
@knz knz requested a review from a team as a code owner May 16, 2022 15:29
@knz knz requested a review from a team May 16, 2022 15:29
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

Copy link
Copy Markdown
Contributor

@kpatron-cockroachlabs kpatron-cockroachlabs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow that's some super weird behavior.

Reviewed 1 of 2 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @catj-cockroach)

@knz
Copy link
Copy Markdown
Contributor Author

knz commented May 16, 2022

TFYR!

bors r=kpatron-cockroachlabs

@craig
Copy link
Copy Markdown
Contributor

craig bot commented May 16, 2022

Build succeeded:

@craig craig bot merged commit 5071fb9 into cockroachdb:master May 16, 2022
knz added a commit to knz/cockroach that referenced this pull request May 28, 2022
This is leftover work from cockroachdb#81298.

Release note: None
@knz knz deleted the 20220516-env branch May 28, 2022 17:47
@knz knz mentioned this pull request Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

security: unnecessary and potentiall insecure call to os.ExpandEnv for --certs-dir

3 participants