forked from mtusi2026/mtusi2026lab7
feat: confine a compromised worker and drill its compromise end to end
- narrow the worker S3 user to staging reads and attachments writes - add database rules the worker role cannot bypass: no return to pending, variant keys of its own attachment, known error codes only - let the cleaner remove a ready source only after its own SHA-256 check - add make jobs_audit [REPAIR=1] to name and requeue damaged jobs - add make worker_compromise: isolated before/after stands, one operation table, response windows, job safety, controls and committed hypotheses Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+163
-41
@@ -3,11 +3,13 @@
|
||||
# политикой «только объекты своего bucket», пользователь уборщика с
|
||||
# политикой «чтение и удаление объектов своего bucket и их список только по
|
||||
# префиксу вложений своей базы» (S3_CLEANER_LIST_PREFIX, <база>/attachments/:
|
||||
# для сверки bucket и БД), пользователь worker с политикой «чтение и запись
|
||||
# объектов своего bucket», проверки приватности вне API. Design change
|
||||
# s3-storage, решения 3, 4 и 8, design change upload-consistency, решение 8,
|
||||
# design change track2-reconciler, решение 2, design change
|
||||
# async-attachments, решение 12; правила — docker.md.
|
||||
# для сверки bucket и БД), пользователь worker с политикой «чтение объектов
|
||||
# <база>/staging/ и запись объектов <база>/attachments/» — своей базы и
|
||||
# test_<база>, проверки приватности вне API. Design change s3-storage,
|
||||
# решения 3, 4 и 8, design change upload-consistency, решение 8, design
|
||||
# change track2-reconciler, решение 2, design change async-attachments,
|
||||
# решение 12, design change track1-worker-compromise, решение 5; правила —
|
||||
# docker.md.
|
||||
#
|
||||
# Запускается сервисом compose s3-bootstrap на образе сервера в сетевом
|
||||
# пространстве сервиса s3 (network_mode: service:s3): master и filer
|
||||
@@ -86,8 +88,11 @@ CHECK_KEY=.s3-check/probe
|
||||
# объект проверок ключа уборщика: его он удаляет, общий нужен проверкам с
|
||||
# хоста
|
||||
CLEANER_CHECK_KEY=.s3-check/cleaner-probe
|
||||
# объект проверок ключа worker: его он пишет; удаляет ключ API
|
||||
WORKER_CHECK_KEY=.s3-check/worker-probe
|
||||
# объекты проверок ключа worker в check — ключами вида API с постоянными id:
|
||||
# повтор перезаписывает остаток прерванного прогона, cleanup его удаляет, а
|
||||
# остаток своей базы без cleanup убрала бы сверка
|
||||
WORKER_STAGING_ID=00000000-0000-4000-8000-00000000c501
|
||||
WORKER_RESULT_ID=00000000-0000-4000-8000-00000000c502
|
||||
CHECK_BUCKET_PREFIX="${BUCKET}-check-"
|
||||
# сколько секунд ждать, пока изменения IAM дойдут до шлюза (подписка на
|
||||
# filer)
|
||||
@@ -233,9 +238,10 @@ wait_cleaner_key() {
|
||||
wait_key 204 "$CLEANER_CRED" -X DELETE "$1"
|
||||
}
|
||||
|
||||
# ключ worker — записью проверочного объекта, как у API
|
||||
# ключ worker — чтением отсутствующего объекта staging: 404 — ключ и право
|
||||
# чтения дошли, 403 — ещё нет
|
||||
wait_worker_key() {
|
||||
wait_key 200 "$WORKER_CRED" -X PUT --data-binary "$TAG" "$1"
|
||||
wait_key 404 "$WORKER_CRED" "$1"
|
||||
}
|
||||
|
||||
# ждать ключ пользователя $1 через функцию $2 с адресом $3; новый секрет
|
||||
@@ -317,14 +323,41 @@ EOF
|
||||
"s3.policy -put -name=${CLEANER_POLICY} -file=$WORK/policy.json"
|
||||
}
|
||||
|
||||
# базы, с объектами которых работает worker: стенда и её тестовая пара (make
|
||||
# test_isolated — под test_<база>/ с теми же ключами)
|
||||
worker_databases() {
|
||||
printf '%s test_%s' "${LIST_PREFIX%/attachments/}" "${LIST_PREFIX%/attachments/}"
|
||||
}
|
||||
|
||||
# ресурс политики worker: объекты префикса $1 (staging, attachments) его баз
|
||||
worker_resources() {
|
||||
list=""
|
||||
for database in $(worker_databases); do
|
||||
list="${list}${list:+, }\"arn:aws:s3:::${BUCKET}/${database}/$1/*\""
|
||||
done
|
||||
printf '[%s]' "$list"
|
||||
}
|
||||
|
||||
# worker читает исходник (staging) и пишет результат (attachments); чтения
|
||||
# результата, записи исходника, удаления и списка нет: исходник принятого
|
||||
# задания он изменить не может (design change track1-worker-compromise,
|
||||
# решение 5)
|
||||
put_worker_policy() {
|
||||
cat >"$WORK/policy.json" <<EOF
|
||||
{"Version": "2012-10-17", "Statement": [{"Effect": "Allow",
|
||||
"Action": ["s3:GetObject"], "Resource": $(worker_resources staging)},
|
||||
{"Effect": "Allow", "Action": ["s3:PutObject"],
|
||||
"Resource": $(worker_resources attachments)}]}
|
||||
EOF
|
||||
ws_step "policy put ${WORKER_POLICY}" \
|
||||
"s3.policy -put -name=${WORKER_POLICY} -file=$WORK/policy.json"
|
||||
}
|
||||
|
||||
apply_policies() {
|
||||
put_policy "$API_POLICY" \
|
||||
'["s3:GetObject", "s3:PutObject", "s3:DeleteObject"]'
|
||||
put_cleaner_policy
|
||||
# worker: читает staging, пишет конечный префикс; удаления и списка нет.
|
||||
# По <bucket>/*, а не по префиксу: make test_isolated работает под
|
||||
# test_<db>/ с теми же ключами (design change async-attachments, решение 12)
|
||||
put_policy "$WORKER_POLICY" '["s3:GetObject", "s3:PutObject"]'
|
||||
put_worker_policy
|
||||
}
|
||||
|
||||
# ключ и секрет пользователя $1 — из окружения (.env)
|
||||
@@ -393,9 +426,9 @@ recreate_key() {
|
||||
# проверки каждого запуска: ключ API пишет и читает проверочный объект,
|
||||
# список и создание bucket — 403; ключ уборщика читает его, запись и
|
||||
# список — 403, затем удаляет; после удаления ключ API получает 404; ключ
|
||||
# worker пишет и читает свой объект, читает объект API (тут же записанный),
|
||||
# удаление, список и создание bucket — 403, объект убирает ключ API;
|
||||
# аноним не читает объект и не видит список
|
||||
# worker читает объект API под staging и пишет под attachments своей базы,
|
||||
# запись в staging, чтение из attachments, удаление, список и создание bucket
|
||||
# — 403, объекты убирает ключ API; аноним не читает объект и не видит список
|
||||
apply_checks() {
|
||||
probe="${S3}/${BUCKET}/.s3-bootstrap/check-$$"
|
||||
wait_user_key "$API_USER" wait_api_key "$probe"
|
||||
@@ -422,15 +455,27 @@ apply_checks() {
|
||||
|| fail "cleaner key list $(staging_list_prefix) (code ${code})"
|
||||
[ "$(cleaner_signed -X DELETE "$probe")" = 204 ] || fail "cleaner key delete"
|
||||
[ "$(signed "$probe")" = 404 ] || fail "object is still there after delete"
|
||||
worker_probe="${S3}/${BUCKET}/.s3-bootstrap/worker-check-$$"
|
||||
wait_user_key "$WORKER_USER" wait_worker_key "$worker_probe"
|
||||
[ "$(worker_signed "$worker_probe")" = 200 ] && [ "$(cat "$BODY")" = "$TAG" ] \
|
||||
|| fail "worker key get"
|
||||
[ "$(signed -X PUT --data-binary "$TAG api" "$probe")" = 200 ] \
|
||||
# ключи вида API: остаток прерванного прогона уберёт сверка
|
||||
object_id=$(cat /proc/sys/kernel/random/uuid)
|
||||
source="${S3}/${BUCKET}/$(staging_list_prefix)${object_id}/original"
|
||||
result="${S3}/${BUCKET}/${LIST_PREFIX}${object_id}/original"
|
||||
wait_user_key "$WORKER_USER" wait_worker_key "$source"
|
||||
# прежняя политика шире: новая доходит до шлюза подпиской, как ключ
|
||||
wait_key 403 "$WORKER_CRED" "$result" \
|
||||
|| fail "worker key get of ${LIST_PREFIX} is not denied (code ${code})"
|
||||
[ "$(signed -X PUT --data-binary "$TAG api" "$source")" = 200 ] \
|
||||
|| fail "api key put for worker"
|
||||
[ "$(worker_signed "$probe")" = 200 ] && [ "$(cat "$BODY")" = "$TAG api" ] \
|
||||
[ "$(worker_signed "$source")" = 200 ] && [ "$(cat "$BODY")" = "$TAG api" ] \
|
||||
|| fail "worker key get object of api key"
|
||||
[ "$(worker_signed -X DELETE "$worker_probe")" = 403 ] \
|
||||
[ "$(worker_signed -X PUT --data-binary "$TAG worker" "$result")" = 200 ] \
|
||||
|| fail "worker key put of ${LIST_PREFIX}"
|
||||
[ "$(signed "$result")" = 200 ] && [ "$(cat "$BODY")" = "$TAG worker" ] \
|
||||
|| fail "api key get object of worker key"
|
||||
[ "$(worker_signed "$result")" = 403 ] \
|
||||
|| fail "worker key get of ${LIST_PREFIX} is not denied"
|
||||
[ "$(worker_signed -X PUT --data-binary "$TAG worker" "$source")" = 403 ] \
|
||||
|| fail "worker key put of $(staging_list_prefix) is not denied"
|
||||
[ "$(worker_signed -X DELETE "$result")" = 403 ] \
|
||||
|| fail "worker key delete is not denied"
|
||||
[ "$(worker_signed "${S3}/${BUCKET}?list-type=2")" = 403 ] \
|
||||
|| fail "worker key list is not denied"
|
||||
@@ -438,12 +483,13 @@ apply_checks() {
|
||||
|| fail "worker key list ${LIST_PREFIX} is not denied"
|
||||
[ "$(worker_signed -X PUT "${S3}/${CHECK_BUCKET_PREFIX}forbidden-$$")" = 403 ] \
|
||||
|| fail "worker key bucket create is not denied"
|
||||
[ "$(signed -X DELETE "$probe")" = 204 ] || fail "api key delete probe"
|
||||
[ "$(signed -X DELETE "$worker_probe")" = 204 ] \
|
||||
[ "$(signed -X DELETE "$source")" = 204 ] || fail "api key delete probe"
|
||||
[ "$(signed -X DELETE "$result")" = 204 ] \
|
||||
|| fail "api key delete worker probe"
|
||||
say "checks passed (api key put/get; cleaner key get/delete and list" \
|
||||
"${LIST_PREFIX} and $(staging_list_prefix); worker key put/get; api key list and bucket create," \
|
||||
"cleaner key put and list of the bucket, worker key delete, list and" \
|
||||
"${LIST_PREFIX} and $(staging_list_prefix); worker key get $(staging_list_prefix) and put ${LIST_PREFIX};" \
|
||||
"api key list and bucket create, cleaner key put and list of the bucket," \
|
||||
"worker key put of staging, get of attachments, delete, list and" \
|
||||
"bucket create, anonymous get and list denied)"
|
||||
}
|
||||
|
||||
@@ -523,17 +569,63 @@ run_checks() {
|
||||
"$(cleaner_signed -X PUT -H 'x-amz-acl: public-read' "${S3}/${BUCKET}?acl")"
|
||||
check "cleaner key put object acl public-read" 403 \
|
||||
"$(cleaner_signed -X PUT -H 'x-amz-acl: public-read' "${obj}?acl")"
|
||||
# ключ worker: чтение и запись объектов своего bucket, в том числе объект,
|
||||
# записанный ключом API
|
||||
worker_own="${S3}/${BUCKET}/${WORKER_CHECK_KEY}"
|
||||
check "worker key put object" 200 \
|
||||
"$(worker_signed -X PUT --data-binary "$TAG worker" "$worker_own")"
|
||||
check "worker key get object" 200 "$(worker_signed "$worker_own")"
|
||||
check "worker key head object" 200 "$(worker_signed -I "$worker_own")"
|
||||
check "worker key get object of api key" 200 "$(worker_signed "$obj")"
|
||||
# ключ worker: удаление, список и всё, что с bucket, запрещено
|
||||
check "worker key delete object" 403 "$(worker_signed -X DELETE "$worker_own")"
|
||||
check "api key get object after worker delete" 200 "$(signed "$worker_own")"
|
||||
# ключ worker (change track1-worker-compromise, решение 5): чтение staging и
|
||||
# запись attachments своей базы и test_<база>; объекты кладёт ключ API
|
||||
for database in $(worker_databases); do
|
||||
source="${S3}/${BUCKET}/$(worker_source_key "$database")"
|
||||
result="${S3}/${BUCKET}/$(worker_result_key "$database")"
|
||||
check "api key put ${database}/staging" 200 \
|
||||
"$(signed -X PUT --data-binary "$TAG api" "$source")"
|
||||
check "worker key get ${database}/staging" 200 "$(worker_signed "$source")"
|
||||
check "worker key head ${database}/staging" 200 \
|
||||
"$(worker_signed -I "$source")"
|
||||
check "worker key put ${database}/attachments" 200 \
|
||||
"$(worker_signed -X PUT --data-binary "$TAG worker" "$result")"
|
||||
check "api key get ${database}/attachments of worker" 200 \
|
||||
"$(signed "$result")"
|
||||
check "worker key get ${database}/attachments" 403 \
|
||||
"$(worker_signed "$result")"
|
||||
check "worker key head ${database}/attachments" 403 \
|
||||
"$(worker_signed -I "$result")"
|
||||
check "worker key put ${database}/staging" 403 \
|
||||
"$(worker_signed -X PUT --data-binary "$TAG worker" "$source")"
|
||||
check "worker key delete ${database}/attachments" 403 \
|
||||
"$(worker_signed -X DELETE "$result")"
|
||||
check "worker key delete ${database}/staging" 403 \
|
||||
"$(worker_signed -X DELETE "$source")"
|
||||
done
|
||||
# ключ worker: свой префикс не открывает остального bucket. Чужие объекты
|
||||
# кладёт ключ API: широкая политика дала бы 200, а не 404
|
||||
database=${LIST_PREFIX%/attachments/}
|
||||
source="${S3}/${BUCKET}/$(worker_source_key "$database")"
|
||||
result="${S3}/${BUCKET}/$(worker_result_key "$database")"
|
||||
foreign="${S3}/${BUCKET}/$(worker_source_key "${database}2")"
|
||||
check "api key put other database" 200 \
|
||||
"$(signed -X PUT --data-binary "$TAG api" "$foreign")"
|
||||
check "worker key get other database" 403 "$(worker_signed "$foreign")"
|
||||
check "worker key put other database" 403 \
|
||||
"$(worker_signed -X PUT --data-binary "$TAG" \
|
||||
"${S3}/${BUCKET}/$(worker_result_key "${database}2")")"
|
||||
check "worker key get bucket root" 403 "$(worker_signed "$obj")"
|
||||
check "worker key put bucket root" 403 \
|
||||
"$(worker_signed -X PUT --data-binary "$TAG" "$obj")"
|
||||
check "worker key copy from attachments" 403 \
|
||||
"$(worker_signed -X PUT -H "x-amz-copy-source: /${BUCKET}/$(worker_result_key "$database")" \
|
||||
"${result}-copy")"
|
||||
check "worker key copy from other database" 403 \
|
||||
"$(worker_signed -X PUT -H "x-amz-copy-source: /${BUCKET}/$(worker_source_key "${database}2")" \
|
||||
"${result}-copy")"
|
||||
# источник копии с «..» шлюз отвергает раньше прав; адрес с «..» curl с
|
||||
# подписью не умеет — его строки в таблице make worker_compromise
|
||||
check "worker key copy from attachments by dotted path" 400 \
|
||||
"$(worker_signed -X PUT -H "x-amz-copy-source: /${BUCKET}/$(staging_list_prefix)../attachments/${WORKER_RESULT_ID}/original" \
|
||||
"${result}-copy")"
|
||||
# пакетное удаление отвечает 200 с ошибкой каждого ключа
|
||||
check "worker key batch delete" "200 AccessDenied" \
|
||||
"$(worker_batch_delete "$(worker_source_key "$database")")"
|
||||
check "api key get staging after worker requests" 200 "$(signed "$source")"
|
||||
check "staging content after worker requests" "$TAG api" "$(cat "$BODY")"
|
||||
# ключ worker: список и всё, что с bucket, запрещено
|
||||
check "worker key list objects" 403 \
|
||||
"$(worker_signed "${S3}/${BUCKET}?list-type=2")"
|
||||
check "worker key list attachments prefix" 403 \
|
||||
@@ -551,7 +643,7 @@ run_checks() {
|
||||
check "worker key put bucket acl public-read" 403 \
|
||||
"$(worker_signed -X PUT -H 'x-amz-acl: public-read' "${S3}/${BUCKET}?acl")"
|
||||
check "worker key put object acl public-read" 403 \
|
||||
"$(worker_signed -X PUT -H 'x-amz-acl: public-read' "${worker_own}?acl")"
|
||||
"$(worker_signed -X PUT -H 'x-amz-acl: public-read' "${result}?acl")"
|
||||
# чужой bucket: временный, создаёт и удаляет weed shell
|
||||
TEMP_BUCKET="${CHECK_BUCKET_PREFIX}$(od -An -N4 -tx4 /dev/urandom | tr -d ' ')"
|
||||
ws_step "temporary bucket create" "s3.bucket.create -name ${TEMP_BUCKET}"
|
||||
@@ -593,8 +685,38 @@ run_checks() {
|
||||
say "checks passed; probe object ${BUCKET}/${CHECK_KEY} kept for host and network checks"
|
||||
}
|
||||
|
||||
# ключи проверочных объектов worker в базе $1 — вида API
|
||||
worker_source_key() {
|
||||
printf '%s/staging/%s/original' "$1" "$WORKER_STAGING_ID"
|
||||
}
|
||||
|
||||
worker_result_key() {
|
||||
printf '%s/attachments/%s/original' "$1" "$WORKER_RESULT_ID"
|
||||
}
|
||||
|
||||
# пакетное удаление ключа $1 ключом worker: код и исход ключа
|
||||
worker_batch_delete() {
|
||||
printf '<Delete><Object><Key>%s</Key></Object></Delete>' "$1" \
|
||||
>"$WORK/delete.xml"
|
||||
got=$(worker_signed -X POST -H 'Content-Type: application/xml' \
|
||||
--data-binary "@$WORK/delete.xml" "${S3}/${BUCKET}?delete=")
|
||||
if grep -q '<Deleted>' "$BODY"; then
|
||||
printf '%s Deleted' "$got"
|
||||
elif grep -q '<Code>AccessDenied</Code>' "$BODY"; then
|
||||
printf '%s AccessDenied' "$got"
|
||||
else
|
||||
printf '%s' "$got"
|
||||
fi
|
||||
}
|
||||
|
||||
run_cleanup() {
|
||||
for key in "$CHECK_KEY" "$CLEANER_CHECK_KEY" "$WORKER_CHECK_KEY"; do
|
||||
worker_keys=""
|
||||
for database in $(worker_databases) "${LIST_PREFIX%/attachments/}2"; do
|
||||
worker_keys="${worker_keys} $(worker_source_key "$database")"
|
||||
worker_keys="${worker_keys} $(worker_result_key "$database")"
|
||||
done
|
||||
for key in "$CHECK_KEY" "$CLEANER_CHECK_KEY" $worker_keys \
|
||||
"$(worker_result_key "${LIST_PREFIX%/attachments/}")-copy"; do
|
||||
code=$(signed -X DELETE "${S3}/${BUCKET}/${key}")
|
||||
case "$code" in
|
||||
204 | 404) say "probe object ${BUCKET}/${key} removed" ;;
|
||||
@@ -610,7 +732,7 @@ run_cleanup() {
|
||||
}
|
||||
|
||||
case "$MODE" in
|
||||
apply | check) check_list_prefix ;;
|
||||
apply | check | cleanup) check_list_prefix ;;
|
||||
esac
|
||||
|
||||
case "$MODE" in
|
||||
|
||||
Reference in New Issue
Block a user