Skip to content

Commit 684618d

Browse files
committed
fix: open log file with mode 0600 and remove permission widening fallback
The agent runs as root, so if it cannot open its own log file, chmod/chown workarounds won't help — just fail. Removed the fallback that ran chmod 666 which made the log world-writable. Now opens with 0600 and returns the error directly on failure.
1 parent 5f2fd86 commit 684618d

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

components/arc/v20260301/arc_registration.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ func (a *installArcAction) addAuthenticationArgs(ctx context.Context, args *[]st
169169
// The token is short-lived (~60 minutes) which limits the exposure window, but it is
170170
// still observable during the registration process. azcmagent does not currently support
171171
// reading the token from stdin or an environment variable.
172-
// Consider switching to --service-principal-cert or --use-azcli when feasible.
173-
// See: https://learn.microsoft.com/en-us/azure/azure-arc/servers/azcmagent-connect
172+
// TODO: Check if we can let azcmagent discover auth settings on its own (e.g. --use-azcli
173+
// or VM MSI) instead of fetching a token ourselves and passing it on the command line.
174174
*args = append(*args, "--access-token", accessToken)
175175

176176
a.logger.Debug("Authentication arguments added to Arc agent command")

pkg/logger/logger.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,10 @@ func setupLogFileWriter(logDir string) (io.Writer, error) {
158158
return nil, fmt.Errorf("failed to create log file '%s': %w", logFilePath, err)
159159
}
160160

161-
// Try to open log file for writing, handle permission issues
162-
file, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_APPEND, 0666)
161+
// Open log file for appending (mode arg is ignored without O_CREATE — permissions
162+
// are set in createLogFileIfNotExists above)
163+
file, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_APPEND, 0) //#nosec G304 - logFilePath is from trusted agent config
163164
if err != nil {
164-
// If it's a permission error and we're not running as root, try to fix permissions
165-
if os.IsPermission(err) {
166-
// Try to fix permissions using system command
167-
if fixErr := utils.RunSystemCommand("chmod", "666", logFilePath); fixErr == nil {
168-
// Retry opening the file after fixing permissions
169-
file, err = os.OpenFile(logFilePath, os.O_WRONLY|os.O_APPEND, 0666)
170-
if err == nil {
171-
return file, nil
172-
}
173-
}
174-
}
175165
return nil, fmt.Errorf("failed to open log file '%s': %w", logFilePath, err)
176166
}
177167

@@ -218,7 +208,7 @@ func createLogFileIfNotExists(logFilePath string) error {
218208
// should have the correct user/group and the log directory should already exist
219209
if isRunningUnderSystemd() {
220210
// Try direct file creation with appropriate permissions
221-
file, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY, 0644)
211+
file, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY, 0600) //#nosec G304 - logFilePath is from trusted agent config
222212
if err == nil {
223213
_ = file.Close()
224214
return nil
@@ -228,7 +218,7 @@ func createLogFileIfNotExists(logFilePath string) error {
228218
}
229219

230220
// Use WriteFileAtomicSystem to create an empty log file with proper permissions
231-
if err := utilio.WriteFile(logFilePath, []byte{}, 0644); err != nil {
221+
if err := utilio.WriteFile(logFilePath, []byte{}, 0600); err != nil {
232222
return err
233223
}
234224

0 commit comments

Comments
 (0)