-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Elastic Agent] Set permissions during installation (#26665)
* Change permissions on install on Windows * Update go.sum. * Add perms for unix. * Fix format. * Add changelog entry. * Fix imports. * Fix go.sum. * Add comments for access masks. * Fix control listener to use Administrators GUID vs name lookup * Set SecurityDescriptor for starting the Elastic Agent local metrics endpoint * Fix format * Fix lookup in libbeat for Administrators group (cherry picked from commit 48a4703)
- Loading branch information
1 parent
702646a
commit 3c54d3b
Showing
8 changed files
with
118 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
// +build !windows | ||
|
||
package install | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths" | ||
) | ||
|
||
// fixPermissions fixes the permissions so only root:root is the owner and no world read-able permissions | ||
func fixPermissions() error { | ||
return recursiveRootPermissions(paths.InstallPath) | ||
} | ||
|
||
func recursiveRootPermissions(path string) error { | ||
return filepath.Walk(path, func(name string, info fs.FileInfo, err error) error { | ||
if err == nil { | ||
// all files should be owned by root:root | ||
err = os.Chown(name, 0, 0) | ||
if err != nil { | ||
return err | ||
} | ||
// remove any world permissions from the file | ||
err = os.Chmod(name, info.Mode().Perm()&0770) | ||
} | ||
return err | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
// +build windows | ||
|
||
package install | ||
|
||
import ( | ||
"io/fs" | ||
"path/filepath" | ||
|
||
"github.com/hectane/go-acl" | ||
"golang.org/x/sys/windows" | ||
|
||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths" | ||
) | ||
|
||
// fixPermissions fixes the permissions so only SYSTEM and Administrators have access to the files in the install path | ||
func fixPermissions() error { | ||
return recursiveSystemAdminPermissions(paths.InstallPath) | ||
} | ||
|
||
func recursiveSystemAdminPermissions(path string) error { | ||
return filepath.Walk(path, func(name string, info fs.FileInfo, err error) error { | ||
if err == nil { | ||
// first level doesn't inherit | ||
inherit := true | ||
if path == name { | ||
inherit = false | ||
} | ||
err = systemAdministratorsOnly(name, inherit) | ||
} | ||
return err | ||
}) | ||
} | ||
|
||
func systemAdministratorsOnly(path string, inherit bool) error { | ||
// https://support.microsoft.com/en-us/help/243330/well-known-security-identifiers-in-windows-operating-systems | ||
systemSID, err := windows.StringToSid("S-1-5-18") | ||
if err != nil { | ||
return err | ||
} | ||
administratorsSID, err := windows.StringToSid("S-1-5-32-544") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// https://docs.microsoft.com/en-us/windows/win32/secauthz/access-mask | ||
return acl.Apply( | ||
path, true, inherit, | ||
acl.GrantSid(0xF10F0000, systemSID), // full control of all acl's | ||
acl.GrantSid(0xF10F0000, administratorsSID)) | ||
} |