@@ -24,14 +24,15 @@ import (
24
24
type Config struct {
25
25
AllowStrings []string
26
26
LogLevel string
27
+ Unprivileged bool
27
28
}
28
29
29
30
// NewCommand creates and returns the root serpent command
30
31
func NewCommand () * serpent.Command {
31
32
// To make the top level jail command, we just make some minor changes to the base command
32
33
cmd := BaseCommand ()
33
34
cmd .Use = "jail [flags] -- command [args...]" // Add the flags and args pieces to usage.
34
-
35
+
35
36
// Add example usage to the long description. This is different from usage as a subcommand because it
36
37
// may be called something different when used as a subcommand / there will be a leading binary (i.e. `coder jail` vs. `jail`).
37
38
cmd .Long += `Examples:
@@ -43,7 +44,7 @@ func NewCommand() *serpent.Command {
43
44
44
45
# Block everything by default (implicit)`
45
46
46
- return cmd
47
+ return cmd
47
48
}
48
49
49
50
// Base command returns the jail serpent command without the information involved in making it the
@@ -74,6 +75,13 @@ user-defined rules.`,
74
75
Default : "warn" ,
75
76
Value : serpent .StringOf (& config .LogLevel ),
76
77
},
78
+ {
79
+ Name : "unprivileged" ,
80
+ Flag : "unprivileged" ,
81
+ Env : "JAIL_UNPRIVILEGED" ,
82
+ Description : "Use unprivileged mode (proxy environment variables, no sudo required, Linux only)." ,
83
+ Value : serpent .BoolOf (& config .Unprivileged ),
84
+ },
77
85
},
78
86
Handler : func (inv * serpent.Invocation ) error {
79
87
return Run (inv .Context (), config , inv .Args )
@@ -112,7 +120,7 @@ func Run(ctx context.Context, config Config, args []string) error {
112
120
auditor := audit .NewLoggingAuditor (logger )
113
121
114
122
// Create certificate manager
115
- certManager , err := tls .NewCertificateManager (tls.Config {
123
+ certManager , err := tls .NewCertificateManager (tls.Options {
116
124
Logger : logger ,
117
125
ConfigDir : userInfo .ConfigDir ,
118
126
})
@@ -171,30 +179,29 @@ func Run(ctx context.Context, config Config, args []string) error {
171
179
return nil
172
180
}
173
181
182
+ // getUserInfo returns information about the current user, handling sudo scenarios
174
183
func getUserInfo () namespace.UserInfo {
175
- // get the user info of the original user even if we are running under sudo
176
- sudoUser := os . Getenv ( " SUDO_USER" )
177
-
178
- // If running under sudo, get original user information
179
- if sudoUser != "" {
184
+ // Only consider SUDO_USER if we're actually running with elevated privileges
185
+ // In environments like Coder workspaces, SUDO_USER may be set to 'root'
186
+ // but we're not actually running under sudo
187
+ if sudoUser := os . Getenv ( "SUDO_USER" ); sudoUser != "" && os . Geteuid () == 0 && sudoUser != "root" {
188
+ // We're actually running under sudo with a non-root original user
180
189
user , err := user .Lookup (sudoUser )
181
190
if err != nil {
182
- // Fallback to current user if lookup fails
183
- return getCurrentUserInfo ()
191
+ return getCurrentUserInfo () // Fallback to current user
184
192
}
185
193
186
- // Parse SUDO_UID and SUDO_GID
187
- uid := 0
188
- gid := 0
194
+ uid , _ := strconv .Atoi (os .Getenv ("SUDO_UID" ))
195
+ gid , _ := strconv .Atoi (os .Getenv ("SUDO_GID" ))
189
196
190
- if sudoUID := os .Getenv ("SUDO_UID" ); sudoUID != "" {
191
- if parsedUID , err := strconv .Atoi (sudoUID ); err == nil {
197
+ // If we couldn't get UID/GID from env, parse from user info
198
+ if uid == 0 {
199
+ if parsedUID , err := strconv .Atoi (user .Uid ); err == nil {
192
200
uid = parsedUID
193
201
}
194
202
}
195
-
196
- if sudoGID := os .Getenv ("SUDO_GID" ); sudoGID != "" {
197
- if parsedGID , err := strconv .Atoi (sudoGID ); err == nil {
203
+ if gid == 0 {
204
+ if parsedGID , err := strconv .Atoi (user .Gid ); err == nil {
198
205
gid = parsedGID
199
206
}
200
207
}
@@ -210,7 +217,7 @@ func getUserInfo() namespace.UserInfo {
210
217
}
211
218
}
212
219
213
- // Not running under sudo, use current user
220
+ // Not actually running under sudo, use current user
214
221
return getCurrentUserInfo ()
215
222
}
216
223
0 commit comments