✳ Field lesson CL1a / 11 min

A function runs with a role

In serverless, the provider patches the operating system and the runtime. The code, its libraries, the execution role, the trigger, and the data stay yours. A function can do exactly what its role allows, so scope the role to the job.

cloudserverlessleast-privilegeinput-validationCISSP D3Security+ (SY0-701) D3Security+ (SY0-801) D3

Helpful first: Who may act in a cloud account · Trust boundaries: the server decides

Thirty days to Halcyon · Case 12: The long list · Episode 2 of 5

Monday afternoon. Dax Mercer, Software Engineer on the operations tools team, shows you pass-report-export. It runs on every new object under halcyon-downlink/incoming/ and writes a one-page summary. He wrote it in an afternoon.

Kael Cho, Staff Infrastructure Engineer, reads its policy. "You copied the node role's old policy."

"It works," Dax says. "And there's no server to break into." Kael uploads a test object with ../ in its name. The summary lands outside reports/. What else could this function do?

By the end you can

  • Say which parts of a serverless function the provider secures and which stay with the customer
  • Scope a function's execution role to the one resource and action its job needs
  • Treat event data that triggers a function as untrusted input

What the provider runs, and what stays yours

A serverless function is code that the provider runs when an event arrives. pass-report-export runs on AWS Lambda, in the same account as halcyon-downlink. Nobody at Meridian logs in to the machine under it, or even knows which machine that is.

The provider takes over a lot. For a managed runtime, Lambda patches the operating system and the language runtime. It starts, stops, and replaces the environments the code runs in. This is shared responsibility with more of the work on the provider's side.

Four things stay with Meridian. The code and every library packaged with it are Dax's to update. The execution role decides what the function can touch. The trigger decides what can start it. The data it reads is only as trustworthy as whoever wrote it.

If Dax packaged his own runtime or a container image instead, patching that would be Meridian's job too.

SORT IT OUT

Dax lists five jobs for pass-report-export on Lambda's managed Python runtime. Who does each one?

  1. Patch the operating system under the function.

  2. Apply a security fix to the Python runtime.

  3. Update the PDF library packaged with the code.

  4. Decide which buckets the function may read and write.

  5. Check that an object name is safe before using it.

Show the answers
  1. AWS. On a managed runtime, Lambda patches the operating system.
  2. AWS. Lambda keeps managed runtimes up to date with patches.
  3. Meridian. Anything in the deployment package is the customer's to update.
  4. Meridian. The execution role is written by the customer.
  5. Meridian. Validating event data is part of the customer's code.

The execution role is the function's whole authority

Set the function beside an ingest node again, a server that Meridian runs itself. Two jobs moved to the provider. The other four did not move at all.

Moving from a server to a function hands the operating system and the runtime to the provider. The libraries, the role, the trigger, and the data stay Meridian's either way.

The most important of the four is the execution role. When the function runs, Lambda takes on this role and hands the code temporary credentials for it. Everything the function can do in the account comes from that role. A function is as powerful as its role.

Dax copied the node role's policy before Kael narrowed it that morning. So his copy still allowed s3:* on every bucket. The job needs two actions. It reads each new object under halcyon-downlink/incoming/ and writes one summary under halcyon-downlink/reports/.

Every other permission is reach that only a bug or an attacker would use. Least privilege for a function means the role lists those two actions on those two prefixes, plus writing its own logs.

WORKED EXAMPLE

Narrow the role to the job

  1. List the job. Read one object under incoming/. Write one summary under reports/. Write its own logs.
  2. Write the grant. s3:GetObject on halcyon-downlink/incoming/*. s3:PutObject on halcyon-downlink/reports/*. Log writing for its own log group only.
  3. Count what went. The copied policy allowed every S3 action on every bucket, including deletes and changes to bucket policies. The new one allows 2 S3 actions, each on one prefix.
  4. Test it. Kael uploads his ../ test object again. The function tries to write outside reports/ and gets access denied.

Result: The function still does its whole job. The ../ bug is still in the code, but the role no longer lets it write anywhere else.

An event is input from whoever wrote the object

A function's trigger is an event, and the event carries data. When an object lands in incoming/, the event tells pass-report-export the bucket, the object's full name, and its size. The trigger covers incoming/ only. On the whole bucket, every summary the function wrote would start it again, in a loop.

Dax's code builds the summary's name from the object's name. It takes the part after incoming/ and puts it after reports/. A path helper then tidies the result, and it resolves ../ as "go up one level". So an object named incoming/../status/pass-118 produces a summary at status/pass-118, outside reports/.

Anyone who can write to incoming/ chooses that name. The upload is a trust boundary, and the event is untrusted input, like a form field on a web page.

So the code checks the name against the pattern a pass file uses and refuses anything else. The narrowed role is a second, independent layer. If a check is ever missed, the write outside reports/ still fails.

SPOT THE PROBLEM

This is part of the event that started pass-report-export for Kael's test object. Flag the fields whose values the uploader chose.

Show the answers
  • object.key: incoming/../status/pass-118 The object's name is whatever the uploader typed, including ../ sequences.
  • object.size: 48213 The uploader decides how many bytes to send, so the size is theirs too. Cap what the function will read.

A warm start can carry data over

Lambda does not always build a fresh environment for each event. After a run, it keeps the environment for a while in case another event arrives. A later run that reuses it is called a warm start.

Files written to /tmp stay there between those runs. AWS documents this, and also says not to count on it. An environment may serve many runs, or be replaced at any time. So code must not assume /tmp is empty, and must not assume it is kept.

pass-report-export downloads each pass to /tmp to read it. Dax's first version never deleted the files. A later run could find an earlier pass still on disk. A name bug like the ../ one could then read that pass into the wrong summary.

Kael asks for two small changes. The function deletes its file when each run ends. It also writes each download under a fresh random name, so one run never opens another run's file by accident.

CHECK YOUR JUDGMENT

Dax writes a second function, pass-archive-copy. It copies each new object in halcyon-downlink/incoming/ to a backup bucket. He asks which change matters most before it goes live.

CASE FILE · CASE 12 · ENTRY 2 OF 5

Answer the closing question correctly to add this entry to your case file.

A function is its role

A crafted object name moved pass-report-export's summaries outside reports/, and its copied every-bucket policy allowed that until its role was cut to reading incoming/ and writing reports/.

Next episode: Dax's function is narrowed. The 1,212 findings still need ranking.

Open the case file ↗

Review sheet: exam notes and quick reference

Find your next idea.

Tip: press / to open search. Escape closes this window.