mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(lambda): mount hot-reload read-only and refuse Docker socket directories (#4154)
Bind-mount the hot-reload host directory at /var/task read-only, as /var/task is in AWS Lambda, and, when no allow-list is set, refuse the host root, /var, /proc, and anything under /run or /var/run, where the Docker socket lives or can be reached. Move the no-allow-list warning onto StartupEvent, beside NetworkExposureGuard, so it reaches an operator who has not called Lambda yet.
This commit is contained in:
1 parent
7cd441231f
commit
83ae2b4697
8 files changed
+258
-12
No files matched your search
@@ -0,0 +1,41 @@
|
||||
package io.github.hectorvent.floci.config;
|
||||
|
||||
import io.quarkus.runtime.StartupEvent;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.event.Observes;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
/**
|
||||
* Warns at startup when Lambda hot-reload is enabled with no allow-list, because any absolute host
|
||||
* path is then a legal bind-mount target. Unlike {@link NetworkExposureGuard} this only warns: the
|
||||
* combination is the documented default of the published compose file, so refusing to start would
|
||||
* break it.
|
||||
*
|
||||
* <p>Startup rather than first use: the operator who most needs the message is the one who has not
|
||||
* touched Lambda yet.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class HotReloadExposureWarning {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(HotReloadExposureWarning.class);
|
||||
|
||||
private final EmulatorConfig config;
|
||||
|
||||
@Inject
|
||||
public HotReloadExposureWarning(EmulatorConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
void onStart(@Observes StartupEvent ignored) {
|
||||
if (hotReloadAcceptsAnyPath(config)) {
|
||||
LOG.warn("Lambda hot-reload is enabled without FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ALLOWED_PATHS: "
|
||||
+ "any absolute path on the Docker host can be bind-mounted into a function container");
|
||||
}
|
||||
}
|
||||
|
||||
static boolean hotReloadAcceptsAnyPath(EmulatorConfig config) {
|
||||
EmulatorConfig.LambdaServiceConfig.HotReload hotReload = config.services().lambda().hotReload();
|
||||
return hotReload.enabled() && hotReload.allowedPaths().isEmpty();
|
||||
}
|
||||
}
|
||||
@@ -283,15 +283,6 @@ public class LambdaService implements ResourceProvider {
|
||||
void init() {
|
||||
initializeStorage();
|
||||
rehydrateConcurrency();
|
||||
warnWhenHotReloadHasNoAllowList();
|
||||
}
|
||||
|
||||
private void warnWhenHotReloadHasNoAllowList() {
|
||||
if (config != null && config.services().lambda().hotReload().enabled()
|
||||
&& config.services().lambda().hotReload().allowedPaths().isEmpty()) {
|
||||
LOG.warn("Lambda hot-reload is enabled without FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ALLOWED_PATHS: "
|
||||
+ "any absolute path on the Docker host can be bind-mounted into a function container");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2992,6 +2983,11 @@ public class LambdaService implements ResourceProvider {
|
||||
"Path '" + hostPath + "' is not under an allowed hot-reload mount prefix.", 400);
|
||||
}
|
||||
});
|
||||
if (config.services().lambda().hotReload().allowedPaths().isEmpty() && reachesDockerSocketDirectory(normalized)) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"Path '" + hostPath + "' can expose the Docker socket. Set "
|
||||
+ "FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ALLOWED_PATHS to mount it.", 400);
|
||||
}
|
||||
String resolvedHostPath = toDockerHostPath(normalized);
|
||||
fn.setHotReloadHostPath(resolvedHostPath);
|
||||
fn.setCodeLocalPath(null);
|
||||
@@ -3011,6 +3007,19 @@ public class LambdaService implements ResourceProvider {
|
||||
return path.length() == 0 ? "/" : path.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* True for the host root, {@code /var}, and anything in {@code /run} or {@code /var/run}, where the
|
||||
* Docker socket lives. {@code /proc} counts too: {@code /proc/1/root/var/run} is a different string
|
||||
* for the same directory, and no code directory lives there either.
|
||||
*/
|
||||
static boolean reachesDockerSocketDirectory(Path normalized) {
|
||||
return normalized.getNameCount() == 0
|
||||
|| normalized.equals(Path.of("/var"))
|
||||
|| normalized.startsWith(Path.of("/run"))
|
||||
|| normalized.startsWith(Path.of("/var/run"))
|
||||
|| normalized.startsWith(Path.of("/proc"));
|
||||
}
|
||||
|
||||
private static boolean isUnderHotReloadPrefix(Path normalizedPath, String prefix) {
|
||||
if (prefix == null || prefix.isBlank()) {
|
||||
return false;
|
||||
|
||||
+1
-1
@@ -291,7 +291,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
// and the create->start copy block agree.
|
||||
boolean useCodeVolume = false;
|
||||
if (fn.isHotReload()) {
|
||||
specBuilder.withBind(fn.getHotReloadHostPath(), TASK_DIR);
|
||||
specBuilder.withReadOnlyBind(fn.getHotReloadHostPath(), TASK_DIR);
|
||||
} else if (fn.getCodeLocalPath() != null) {
|
||||
useCodeVolume = shouldUseCodeVolume(Path.of(fn.getCodeLocalPath()));
|
||||
if (useCodeVolume) {
|
||||
|
||||
Reference in new issue
Block a user