1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
|
package main
import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"sync"
"github.com/gorilla/mux"
"golang.org/x/sys/unix"
"github.com/lxc/incus/v6/internal/linux"
"github.com/lxc/incus/v6/internal/server/events"
"github.com/lxc/incus/v6/internal/server/instance"
"github.com/lxc/incus/v6/internal/server/instance/instancetype"
"github.com/lxc/incus/v6/internal/server/lifecycle"
"github.com/lxc/incus/v6/internal/server/request"
"github.com/lxc/incus/v6/internal/server/response"
"github.com/lxc/incus/v6/internal/server/state"
"github.com/lxc/incus/v6/internal/server/ucred"
"github.com/lxc/incus/v6/internal/version"
"github.com/lxc/incus/v6/shared/api"
apiGuest "github.com/lxc/incus/v6/shared/api/guest"
"github.com/lxc/incus/v6/shared/logger"
"github.com/lxc/incus/v6/shared/util"
"github.com/lxc/incus/v6/shared/ws"
)
type hoistFunc func(f func(*Daemon, instance.Instance, http.ResponseWriter, *http.Request) response.Response, d *Daemon) func(http.ResponseWriter, *http.Request)
// DevIncusServer creates an http.Server capable of handling requests against the
// /dev/incus Unix socket endpoint created inside containers.
func devIncusServer(d *Daemon) *http.Server {
return &http.Server{
Handler: devIncusAPI(d, hoistReq),
ConnState: pidMapper.ConnStateHandler,
ConnContext: request.SaveConnectionInContext,
}
}
type devIncusHandler struct {
path string
/*
* This API will have to be changed slightly when we decide to support
* websocket events upgrading, but since we don't have events on the
* server side right now either, I went the simple route to avoid
* needless noise.
*/
f func(d *Daemon, c instance.Instance, w http.ResponseWriter, r *http.Request) response.Response
}
var devIncusConfigGet = devIncusHandler{"/1.0/config", func(d *Daemon, c instance.Instance, w http.ResponseWriter, r *http.Request) response.Response {
if util.IsFalse(c.ExpandedConfig()["security.guestapi"]) {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusForbidden, "not authorized"), c.Type() == instancetype.VM)
}
filtered := []string{}
for k := range c.ExpandedConfig() {
if strings.HasPrefix(k, "user.") || strings.HasPrefix(k, "cloud-init.") {
filtered = append(filtered, fmt.Sprintf("/1.0/config/%s", k))
}
}
return response.DevIncusResponse(http.StatusOK, filtered, "json", c.Type() == instancetype.VM)
}}
var devIncusConfigKeyGet = devIncusHandler{"/1.0/config/{key}", func(d *Daemon, c instance.Instance, w http.ResponseWriter, r *http.Request) response.Response {
if util.IsFalse(c.ExpandedConfig()["security.guestapi"]) {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusForbidden, "not authorized"), c.Type() == instancetype.VM)
}
key, err := url.PathUnescape(mux.Vars(r)["key"])
if err != nil {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusBadRequest, "bad request"), c.Type() == instancetype.VM)
}
if !strings.HasPrefix(key, "user.") && !strings.HasPrefix(key, "cloud-init.") {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusForbidden, "not authorized"), c.Type() == instancetype.VM)
}
value, ok := c.ExpandedConfig()[key]
if !ok {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusNotFound, "not found"), c.Type() == instancetype.VM)
}
return response.DevIncusResponse(http.StatusOK, value, "raw", c.Type() == instancetype.VM)
}}
var devIncusImageExport = devIncusHandler{"/1.0/images/{fingerprint}/export", func(d *Daemon, c instance.Instance, w http.ResponseWriter, r *http.Request) response.Response {
if util.IsFalse(c.ExpandedConfig()["security.guestapi"]) {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusForbidden, "not authorized"), c.Type() == instancetype.VM)
}
if util.IsFalseOrEmpty(c.ExpandedConfig()["security.guestapi.images"]) {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusForbidden, "not authorized"), c.Type() == instancetype.VM)
}
// Use by security checks to distinguish /dev/incus vs REST APs
r.RemoteAddr = "@dev_incus"
resp := imageExport(d, r)
err := resp.Render(w)
if err != nil {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusInternalServerError, "internal server error"), c.Type() == instancetype.VM)
}
return response.DevIncusResponse(http.StatusOK, "", "raw", c.Type() == instancetype.VM)
}}
var devIncusMetadataGet = devIncusHandler{"/1.0/meta-data", func(d *Daemon, inst instance.Instance, w http.ResponseWriter, r *http.Request) response.Response {
if util.IsFalse(inst.ExpandedConfig()["security.guestapi"]) {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusForbidden, "not authorized"), inst.Type() == instancetype.VM)
}
value := inst.ExpandedConfig()["user.meta-data"]
return response.DevIncusResponse(http.StatusOK, fmt.Sprintf("#cloud-config\ninstance-id: %s\nlocal-hostname: %s\n%s", inst.CloudInitID(), inst.Name(), value), "raw", inst.Type() == instancetype.VM)
}}
var devIncusEventsGet = devIncusHandler{"/1.0/events", func(d *Daemon, c instance.Instance, w http.ResponseWriter, r *http.Request) response.Response {
if util.IsFalse(c.ExpandedConfig()["security.guestapi"]) {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusForbidden, "not authorized"), c.Type() == instancetype.VM)
}
typeStr := r.FormValue("type")
if typeStr == "" {
typeStr = "config,device"
}
var listenerConnection events.EventListenerConnection
var resp response.Response
// If the client has not requested a websocket connection then fallback to long polling event stream mode.
if r.Header.Get("Upgrade") == "websocket" {
conn, err := ws.Upgrader.Upgrade(w, r, nil)
if err != nil {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusInternalServerError, "internal server error"), c.Type() == instancetype.VM)
}
defer func() { _ = conn.Close() }() // Ensure listener below ends when this function ends.
listenerConnection = events.NewWebsocketListenerConnection(conn)
resp = response.DevIncusResponse(http.StatusOK, "websocket", "websocket", c.Type() == instancetype.VM)
} else {
h, ok := w.(http.Hijacker)
if !ok {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusInternalServerError, "internal server error"), c.Type() == instancetype.VM)
}
conn, _, err := h.Hijack()
if err != nil {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusInternalServerError, "internal server error"), c.Type() == instancetype.VM)
}
defer func() { _ = conn.Close() }() // Ensure listener below ends when this function ends.
listenerConnection, err = events.NewStreamListenerConnection(conn)
if err != nil {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusInternalServerError, "internal server error"), c.Type() == instancetype.VM)
}
resp = response.DevIncusResponse(http.StatusOK, "", "raw", c.Type() == instancetype.VM)
}
listener, err := d.State().DevIncusEvents.AddListener(c.ID(), listenerConnection, strings.Split(typeStr, ","))
if err != nil {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusInternalServerError, "internal server error"), c.Type() == instancetype.VM)
}
logger.Debug("New container event listener", logger.Ctx{"instance": c.Name(), "project": c.Project().Name, "listener_id": listener.ID})
listener.Wait(r.Context())
return resp
}}
var devIncusAPIHandler = devIncusHandler{"/1.0", func(d *Daemon, c instance.Instance, w http.ResponseWriter, r *http.Request) response.Response {
s := d.State()
if r.Method == "GET" {
var location string
if d.serverClustered {
location = c.Location()
} else {
var err error
location, err = os.Hostname()
if err != nil {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusInternalServerError, "internal server error"), c.Type() == instancetype.VM)
}
}
var state api.StatusCode
if util.IsTrue(c.LocalConfig()["volatile.last_state.ready"]) {
state = api.Ready
} else {
state = api.Started
}
return response.DevIncusResponse(http.StatusOK, apiGuest.DevIncusGet{APIVersion: version.APIVersion, Location: location, InstanceType: c.Type().String(), DevIncusPut: apiGuest.DevIncusPut{State: state.String()}}, "json", c.Type() == instancetype.VM)
} else if r.Method == "PATCH" {
if util.IsFalse(c.ExpandedConfig()["security.guestapi"]) {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusForbidden, "not authorized"), c.Type() == instancetype.VM)
}
req := apiGuest.DevIncusPut{}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusBadRequest, "%s", err.Error()), c.Type() == instancetype.VM)
}
state := api.StatusCodeFromString(req.State)
if state != api.Started && state != api.Ready {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusBadRequest, "Invalid state %q", req.State), c.Type() == instancetype.VM)
}
err = c.VolatileSet(map[string]string{"volatile.last_state.ready": strconv.FormatBool(state == api.Ready)})
if err != nil {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusInternalServerError, "%s", err.Error()), c.Type() == instancetype.VM)
}
if state == api.Ready {
s.Events.SendLifecycle(c.Project().Name, lifecycle.InstanceReady.Event(c, nil))
}
return response.DevIncusResponse(http.StatusOK, "", "raw", c.Type() == instancetype.VM)
}
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusMethodNotAllowed, "%s", fmt.Sprintf("method %q not allowed", r.Method)), c.Type() == instancetype.VM)
}}
var devIncusDevicesGet = devIncusHandler{"/1.0/devices", func(d *Daemon, c instance.Instance, w http.ResponseWriter, r *http.Request) response.Response {
if util.IsFalse(c.ExpandedConfig()["security.guestapi"]) {
return response.DevIncusErrorResponse(api.StatusErrorf(http.StatusForbidden, "not authorized"), c.Type() == instancetype.VM)
}
// Populate NIC hwaddr from volatile if not explicitly specified.
// This is so cloud-init running inside the instance can identify the NIC when the interface name is
// different than the device name (such as when run inside a VM).
localConfig := c.LocalConfig()
devices := c.ExpandedDevices()
for devName, devConfig := range devices {
if devConfig["type"] == "nic" && devConfig["hwaddr"] == "" && localConfig[fmt.Sprintf("volatile.%s.hwaddr", devName)] != "" {
devices[devName]["hwaddr"] = localConfig[fmt.Sprintf("volatile.%s.hwaddr", devName)]
}
}
return response.DevIncusResponse(http.StatusOK, c.ExpandedDevices(), "json", c.Type() == instancetype.VM)
}}
var handlers = []devIncusHandler{
{"/", func(d *Daemon, c instance.Instance, w http.ResponseWriter, r *http.Request) response.Response {
return response.DevIncusResponse(http.StatusOK, []string{"/1.0"}, "json", c.Type() == instancetype.VM)
}},
devIncusAPIHandler,
devIncusConfigGet,
devIncusConfigKeyGet,
devIncusMetadataGet,
devIncusEventsGet,
devIncusImageExport,
devIncusDevicesGet,
}
func hoistReq(f func(*Daemon, instance.Instance, http.ResponseWriter, *http.Request) response.Response, d *Daemon) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
conn := ucred.GetConnFromContext(r.Context())
cred, ok := pidMapper.m[conn.(*net.UnixConn)]
if !ok {
http.Error(w, errPIDNotInContainer.Error(), http.StatusInternalServerError)
return
}
s := d.State()
c, err := findContainerForPid(cred.Pid, s)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Access control
rootUID := uint32(0)
idmapset, err := c.CurrentIdmap()
if err == nil && idmapset != nil {
uid, _ := idmapset.ShiftIntoNS(0, 0)
rootUID = uint32(uid)
}
if rootUID != cred.Uid {
http.Error(w, "Access denied for non-root user", http.StatusUnauthorized)
return
}
resp := f(d, c, w, r)
_ = resp.Render(w)
}
}
func devIncusAPI(d *Daemon, f hoistFunc) http.Handler {
router := mux.NewRouter()
router.UseEncodedPath() // Allow encoded values in path segments.
for _, handler := range handlers {
router.HandleFunc(handler.path, f(handler.f, d))
}
return router
}
/*
* Everything below here is the guts of the unix socket bits. Unfortunately,
* golang's API does not make this easy. What happens is:
*
* 1. We install a ConnState listener on the http.Server, which does the
* initial unix socket credential exchange. When we get a connection started
* event, we use SO_PEERCRED to extract the creds for the socket.
*
* 2. We store a map from the connection pointer to the pid for that
* connection, so that once the HTTP negotiation occurs and we get a
* ResponseWriter, we know (because we negotiated on the first byte) which
* pid the connection belongs to.
*
* 3. Regular HTTP negotiation and dispatch occurs via net/http.
*
* 4. When rendering the response via ResponseWriter, we match its underlying
* connection against what we stored in step (2) to figure out which container
* it came from.
*/
/*
* We keep this in a global so that we can reference it from the server and
* from our http handlers, since there appears to be no way to pass information
* around here.
*/
var pidMapper = ConnPidMapper{m: map[*net.UnixConn]*unix.Ucred{}}
type ConnPidMapper struct {
m map[*net.UnixConn]*unix.Ucred
mLock sync.Mutex
}
func (m *ConnPidMapper) ConnStateHandler(conn net.Conn, state http.ConnState) {
unixConn := conn.(*net.UnixConn)
switch state {
case http.StateNew:
cred, err := linux.GetUcred(unixConn)
if err != nil {
logger.Debugf("Error getting ucred for conn %s", err)
} else {
m.mLock.Lock()
m.m[unixConn] = cred
m.mLock.Unlock()
}
case http.StateActive:
return
case http.StateIdle:
return
case http.StateHijacked:
/*
* The "Hijacked" state indicates that the connection has been
* taken over from net/http. This is useful for things like
* developing websocket libraries, who want to upgrade the
* connection to a websocket one, and not use net/http any
* more. Whatever the case, we want to forget about it since we
* won't see it either.
*/
m.mLock.Lock()
delete(m.m, unixConn)
m.mLock.Unlock()
case http.StateClosed:
m.mLock.Lock()
delete(m.m, unixConn)
m.mLock.Unlock()
default:
logger.Debugf("Unknown state for connection %s", state)
}
}
var errPIDNotInContainer = errors.New("pid not in container?")
func findContainerForPid(pid int32, s *state.State) (instance.Container, error) {
/*
* Try and figure out which container a pid is in. There is probably a
* better way to do this. Based on rharper's initial performance
* metrics, looping over every container and loading them is
* expensive, so I wanted to avoid that if possible, so this happens in
* a two step process:
*
* 1. Walk up the process tree until you see something that looks like
* an lxc monitor process and extract its name from there.
*
* 2. If this fails, it may be that someone did an `incus exec foo -- bash`,
* so the process isn't actually a descendant of the container's
* init. In this case we just look through all the containers until
* we find an init with a matching pid namespace. This is probably
* uncommon, so hopefully the slowness won't hurt us.
*/
origpid := pid
for pid > 1 {
cmdline, err := os.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
if err != nil {
return nil, err
}
status, err := os.ReadFile(fmt.Sprintf("/proc/%d/status", pid))
if err != nil {
return nil, err
}
if strings.HasPrefix(string(cmdline), "[lxc monitor]") && strings.Contains(string(status), fmt.Sprintf("NSpid: %d\n", pid)) {
// container names can't have spaces
parts := strings.Split(string(cmdline), " ")
name := strings.TrimSuffix(parts[len(parts)-1], "\x00")
projectName := api.ProjectDefaultName
if strings.Contains(name, "_") {
fields := strings.SplitN(name, "_", 2)
projectName = fields[0]
name = fields[1]
}
inst, err := instance.LoadByProjectAndName(s, projectName, name)
if err != nil {
return nil, err
}
if inst.Type() != instancetype.Container {
return nil, errors.New("Instance is not container type")
}
return inst.(instance.Container), nil
}
re, err := regexp.Compile(`^PPid:\s+([0-9]+)$`)
if err != nil {
return nil, err
}
for _, line := range strings.Split(string(status), "\n") {
m := re.FindStringSubmatch(line)
if len(m) > 1 {
result, err := strconv.Atoi(m[1])
if err != nil {
return nil, err
}
pid = int32(result)
break
}
}
}
origPidNs, err := os.Readlink(fmt.Sprintf("/proc/%d/ns/pid", origpid))
if err != nil {
return nil, err
}
instances, err := instance.LoadNodeAll(s, instancetype.Container)
if err != nil {
return nil, err
}
for _, inst := range instances {
if inst.Type() != instancetype.Container {
continue
}
if !inst.IsRunning() {
continue
}
initpid := inst.InitPID()
pidNs, err := os.Readlink(fmt.Sprintf("/proc/%d/ns/pid", initpid))
if err != nil {
return nil, err
}
if origPidNs == pidNs {
return inst.(instance.Container), nil
}
}
return nil, errPIDNotInContainer
}
|