Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new config param to allow GroupExec perm for the SecretBackend command #6298

Merged
merged 16 commits into from
Sep 18, 2020

Conversation

clamoriniere
Copy link
Contributor

@clamoriniere clamoriniere commented Aug 28, 2020

What does this PR do?

To allow the execution of the secret-backend command packaged inside
the docker image. the secret-backend script need to allow the execution
of a group uid.
The new option "secret_backend_command_allow_group_exec_perm" allow to
change the behaviour of the checkRights() function and allow GroupExecPerm.

We update only the dockerfile configuration for the cluster-agent. because
the "agent" process needs to run as root, so the described setup is not possible
with the agent image:

  • change the readsecret.sh permission to 510
  • set DD_SECRET_BACKEND_COMMAND_ALLOW_GROUP_EXEC_PERM env var to true
    by default in the cluster-agent entry-point

Motivation

When the agent runs in a container environment, the process can be
executed with a "random" uid (for security reason)

Additional Notes

Anything else we should know when reviewing?

Describe your test plan

Start the Cluster-Agent and use the "secret-backend" script packaged in the image readSecret.py. The Cluster-Agent should not return a file permission check error.

@clamoriniere clamoriniere added this to the 7.23.0 milestone Aug 28, 2020
@clamoriniere clamoriniere requested review from a team as code owners August 28, 2020 15:55
@clamoriniere clamoriniere force-pushed the clamoriniere/fix-checks-permission-v1 branch from 5f889b5 to 850895d Compare August 31, 2020 07:30
@clamoriniere clamoriniere changed the title Allow GroupExec perm for the SecretBackend command Add new config param to allow GroupExec perm for the SecretBackend command Aug 31, 2020
Copy link
Member

@L3n41c L3n41c left a comment

Choose a reason for hiding this comment

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

The biggest issue is the lack of read permission for a script.

Otherwise, I left some cosmetics suggestions.

Dockerfiles/cluster-agent/arm64/Dockerfile Outdated Show resolved Hide resolved
pkg/secrets/check_rights_nix.go Outdated Show resolved Hide resolved
pkg/secrets/check_rights_nix.go Outdated Show resolved Hide resolved
pkg/secrets/check_rights_nix.go Outdated Show resolved Hide resolved
pkg/secrets/check_rights_nix_test.go Outdated Show resolved Hide resolved
pkg/secrets/check_rights_nix_test.go Outdated Show resolved Hide resolved
pkg/secrets/check_rights_nix_test.go Outdated Show resolved Hide resolved
pkg/secrets/no_secrets.go Outdated Show resolved Hide resolved
pkg/secrets/secrets.go Outdated Show resolved Hide resolved
pkg/secrets/secrets.go Outdated Show resolved Hide resolved
@clamoriniere clamoriniere force-pushed the clamoriniere/fix-checks-permission-v1 branch 2 times, most recently from 855c4c7 to fee5f73 Compare August 31, 2020 11:44
@clamoriniere clamoriniere requested a review from L3n41c August 31, 2020 11:44
Copy link
Member

@L3n41c L3n41c left a comment

Choose a reason for hiding this comment

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

There is only one error message which is misleading.

pkg/secrets/check_rights_nix.go Outdated Show resolved Hide resolved
Copy link
Member

@hush-hush hush-hush left a comment

Choose a reason for hiding this comment

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

A few comments. But overall if security is OK with this I think we're almost good to merge.

pkg/secrets/check_rights_nix.go Outdated Show resolved Hide resolved
pkg/secrets/check_rights_nix.go Outdated Show resolved Hide resolved
pkg/secrets/check_rights.go Outdated Show resolved Hide resolved
pkg/secrets/check_rights_windows.go Outdated Show resolved Hide resolved
pkg/secrets/secrets.go Show resolved Hide resolved
@hush-hush
Copy link
Member

Once this is merged let's not forget to update the official doc to explain when, why and how to enable this option : https://docs.datadoghq.com/agent/guide/secrets-management/?tab=linux

@hush-hush hush-hush requested a review from a team September 3, 2020 18:16
pkg/secrets/check_rights_nix.go Outdated Show resolved Hide resolved
pkg/secrets/check_rights_nix.go Outdated Show resolved Hide resolved
@olivielpeau olivielpeau requested review from trishankatdatadog and removed request for a team September 8, 2020 18:15
Copy link
Member

@olivielpeau olivielpeau left a comment

Choose a reason for hiding this comment

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

Left a few nits inline, once addressed LGTM 👍

Let's have a security review as well

pkg/secrets/check_rights_nix.go Outdated Show resolved Hide resolved
pkg/secrets/check_rights_nix.go Outdated Show resolved Hide resolved
pkg/secrets/secrets.go Show resolved Hide resolved
@clamoriniere clamoriniere force-pushed the clamoriniere/fix-checks-permission-v1 branch from 79fe747 to 44fcf25 Compare September 8, 2020 22:47
pkg/secrets/check_rights_windows.go Outdated Show resolved Hide resolved
pkg/secrets/check_rights_nix_test.go Outdated Show resolved Hide resolved
pkg/secrets/check_rights_nix_test.go Show resolved Hide resolved
pkg/secrets/check_rights_nix.go Outdated Show resolved Hide resolved
Copy link
Member

@hush-hush hush-hush left a comment

Choose a reason for hiding this comment

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

I think it's almost good to go for me.

@clamoriniere clamoriniere force-pushed the clamoriniere/fix-checks-permission-v1 branch from 10af196 to 9550d63 Compare September 16, 2020 18:58
Copy link
Member

@hush-hush hush-hush left a comment

Choose a reason for hiding this comment

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

One nit but looks good otherwise

pkg/secrets/check_rights_nix.go Outdated Show resolved Hide resolved
Copy link
Member

@olivielpeau olivielpeau left a comment

Choose a reason for hiding this comment

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

Nits, and one comment on the logged warning, but the logic LGTM! 👍 (and thanks for the additional unit tests!)

}

// checking that we own the executable
usr, err := user.Current()
userGroups, err := usr.GroupIds()
if err != nil {
return fmt.Errorf("can't query current user UID: %s", err)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return fmt.Errorf("can't query current user UID: %s", err)
return fmt.Errorf("can't query current user's GIDs: %s", err)

pkg/secrets/check_rights_nix.go Outdated Show resolved Hide resolved
pkg/secrets/check_rights_nix.go Outdated Show resolved Hide resolved
pkg/secrets/secrets.go Show resolved Hide resolved
pkg/secrets/check_rights_nix_test.go Outdated Show resolved Hide resolved
Copy link
Member

@trishankatdatadog trishankatdatadog left a comment

Choose a reason for hiding this comment

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

Other than warning that a script is executable but not readable (very minor comment), and adding tests for checking UID, I see no security issue. Thanks for all your work!

return nil
}

// checkUserPermission check that only the current User or one of his group can exec the path
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// checkUserPermission check that only the current User or one of his group can exec the path
// checkGroupPermission check that only the current User or one of his group can exec the path

@@ -24,7 +24,9 @@ var (

// checkRights check that the given filename has access controls set only for
// Administrator, Local System and the datadog user.
func checkRights(filename string) error {
func checkRights(filename string, _ bool) error {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe instead of ignoring the bool here, return an error if it is true?

isUserFile = true
}
// If the file is not own by the user, lets check for on of his groups
if !isUserFile {
Copy link
Member

Choose a reason for hiding this comment

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

if isUserFile, you might wish to check that user can exec (check that stat.Mode&syscall.S_IXUSR == 0 is not an issue like in checkUserPermission)

require.Nil(t, os.Chmod(tmpfile.Name(), 0600))
require.NotNil(t, checkRights(tmpfile.Name()))
require.NotNil(t, checkRights(tmpfile.Name(), allowGroupExec))

// group should have no right
require.Nil(t, os.Chmod(tmpfile.Name(), 0710))
Copy link
Member

Choose a reason for hiding this comment

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

Consider adding a separate 0720 test here (group can write)


// group should have no right
require.Nil(t, os.Chmod(tmpfile.Name(), 0710))
require.NotNil(t, checkRights(tmpfile.Name()))
require.NotNil(t, checkRights(tmpfile.Name(), allowGroupExec))

// other should have no right
require.Nil(t, os.Chmod(tmpfile.Name(), 0701))
Copy link
Member

Choose a reason for hiding this comment

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

Consider adding a separate 0702 test here (others can write)

require.Nil(t, os.Chmod(tmpfile.Name(), 0770))
require.NotNil(t, checkRights(tmpfile.Name(), allowGroupExec))

// other should have no right
Copy link
Member

Choose a reason for hiding this comment

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

Consider adding a separate 0702 test here (others can write)

clamoriniere and others added 16 commits September 18, 2020 13:27
When the agent runs in a container environment, the process can be
executed with a "random" uid (for security reason).
To allow the execution of the secret-backend command packaged inside
the docker image. the secret-backend script need to allow the execution
of a group uid.
The new option "secret_backend_command_allow_group_exec_perm" allow to
change the behaviour of the `checkRights()` function and allow GroupExecPerm.

We update only the dockerfile configuration for the cluster-agent. because
the "agent" process needs to run as root, so the described setup is not possible
with the agent image:
* change the readsecret.sh permission to 510
* set DD_SECRET_BACKEND_COMMAND_ALLOW_GROUP_EXEC_PERM env var to true
  by default in the cluster-agent entry-point
Co-authored-by: Lénaïc Huard <L3n41c@users.noreply.github.com>
Co-authored-by: Olivier Vielpeau <olivielpeau@users.noreply.github.com>
Co-authored-by: Trishank Karthik Kuppusamy <trishank.kuppusamy@datadoghq.com>
Signed-off-by: cedric lamoriniere <cedric.lamoriniere@datadoghq.com>
Co-authored-by: maxime mouial <hush-hush@users.noreply.github.com>
Co-authored-by: Olivier Vielpeau <olivielpeau@users.noreply.github.com>
Signed-off-by: cedric lamoriniere <cedric.lamoriniere@datadoghq.com>
@clamoriniere clamoriniere force-pushed the clamoriniere/fix-checks-permission-v1 branch from f587136 to 1c431ca Compare September 18, 2020 11:28
@clamoriniere clamoriniere merged commit b05aa49 into master Sep 18, 2020
@clamoriniere clamoriniere deleted the clamoriniere/fix-checks-permission-v1 branch September 18, 2020 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants