<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pravin Raajendiran on Medium]]></title>
        <description><![CDATA[Stories by Pravin Raajendiran on Medium]]></description>
        <link>https://medium.com/@praajendiran?source=rss-8ef09999d479------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*QZWZsrr9Qy-MNQJ7.</url>
            <title>Stories by Pravin Raajendiran on Medium</title>
            <link>https://medium.com/@praajendiran?source=rss-8ef09999d479------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 02:56:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@praajendiran/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Running untrusted workload with Go]]></title>
            <link>https://zendesk.engineering/running-untrusted-workload-with-go-113f2756d408?source=rss-8ef09999d479------2</link>
            <guid isPermaLink="false">https://medium.com/p/113f2756d408</guid>
            <category><![CDATA[jq]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[isolation]]></category>
            <dc:creator><![CDATA[Pravin Raajendiran]]></dc:creator>
            <pubDate>Mon, 25 Jul 2022 01:58:07 GMT</pubDate>
            <atom:updated>2022-07-25T01:58:07.026Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aN6CGv2Ys2UleDIV" /><figcaption>Photo by <a href="https://unsplash.com/es/@ellaolsson?utm_source=medium&amp;utm_medium=referral">Ella Olsson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>There are times where we require to be able to “contain” a third-party library especially around resource usage. Say, the library you’re using has some operation that results in an unbounded memory growth. In most cases, you’d be able to fix that part of the library that leaks or grows memory without checks, and make a pull request upstream.</p><p>There are some situations where this is not possible at all, e.g. consider an API of a library that takes a JSON object, a <a href="https://stedolan.github.io/jq/manual/">Jq</a> expression to run on the JSON object, and return the response. The expression could be as simple as to “find the value of a key in a given JSON object”, or a bit memory intensive as “Use the value of the key $.foo to make as many copies of $.bar, and assign it to $.baz”. There are multiple ways to craft such expressions that are memory intensive, and therefore it’s harder to maintain a set of “pre-validation” rules on the expressions whose memory growth is only determined at runtime.</p><p>Moreover, when multiple Jq expressions are evaluated concurrently, say, in a goroutine (a lightweight thread that is managed by the goruntime), all it takes is one dodgy expression to get the whole program <a href="https://www.kernel.org/doc/gorman/html/understand/understand016.html">OOM</a> killed since goroutines share the same address space.</p><h3>Zendesk Integration Services</h3><p>My team has built an integration platform called <a href="https://developer.zendesk.com/documentation/integration-services/">Zendesk Integration Services</a> or ZIS that provides a workflow platform for building integrations with zendesk and other systems. The workflow called <a href="https://developer.zendesk.com/documentation/integration-services/getting-started/anatomy-zis-bundle/">ZIS Flow</a> is based on the <a href="https://states-language.net/">state language specification</a> from Amazon. We have slightly extended this specification to <a href="https://developer.zendesk.com/documentation/integration-services/developer-guide/zis-built-in-actions/#transform">support JSON transformation</a> using the well-known Jq tool. We use the pure-go implementation of Jq called <a href="https://github.com/itchyny/gojq">GoJq</a> to power those transformations.</p><p>We execute more than 5 million workflows and 10 million JSON transformations per day, and it’s growing rapidly as we onboard more and more integration use cases each day.</p><p>Each workflow is executed in a goroutine to achieve higher throughput. Currently, the workflow service handles up to 800 workflow executions per second.</p><p>A workflow shall contain one or many transformation steps (or no transformation step at all). A transformation step takes in an input object (data), an expression (expr) to evaluate, and returns an output. An example transformation to convert an int to a string is shown below.</p><pre>&quot;TransformTicketIdToString&quot;: {<br>    &quot;Type&quot;: &quot;Action&quot;,<br>    &quot;ActionName&quot;: &quot;zis:common:transform:Jq&quot;,<br>    &quot;Parameters&quot;: {<br>        &quot;expr&quot;: &quot;.ticket.id | tostring&quot;,<br>        &quot;data.$&quot;: &quot;$.input&quot;<br>    },<br>    &quot;ResultPath&quot;: &quot;$.ticket.id&quot;,<br>    &quot;Next&quot;: &quot;NextState&quot;<br>}</pre><p>Where the input to the workflow is</p><pre>{<br>    &quot;input&quot;: {<br>        &quot;ticket&quot;: {<br>            &quot;id&quot;: 1234<br>        }<br>    }<br>}</pre><p>This TransformTicketIdToString is pretty simple and barely scratches the surface of transformational abilities of Jq.</p><p>There are other transformations that can cause some damage <em>e.g</em>. repeat a string 5 million times; create a large array of objects using a combination of range and flatten functions.</p><p><em>e.g</em>.[[{\”foo\”: \”bar\”}] as $_ | range(1000000) | $_] | flatten this expression creates an array of 1 million objects.</p><p>We’ll see an example of such transformation used in the workflow</p><pre>&quot;NaughtyTransform&quot;: {<br>    &quot;Type&quot;: &quot;Action&quot;,<br>    &quot;ActionName&quot;: &quot;zis:common:transform:Jq&quot;,<br>    &quot;Parameters&quot;: {<br>        &quot;expr&quot;: &quot;.foo * 5000000&quot;,<br>        &quot;data.$&quot;: &quot;$.input&quot;<br>    },<br>    &quot;ResultPath&quot;: &quot;$.baz&quot;,<br>    &quot;Next&quot;: &quot;NextState&quot;<br>}</pre><p>Where the input to the workflow is</p><pre>{<br>    &quot;input&quot;: {<br>       &quot;foo&quot;: &quot;bar&quot;<br>    }<br>}</pre><p>At the end of the NaughtyTransform step, we’ll have a new key called baz whose value will be bar... (<em>repeated 5 million times</em>).</p><pre>{<br>    &quot;input&quot;: {<br>        &quot;foo&quot;: &quot;bar<br>    },<br>    &quot;baz&quot;: &quot;barbarbar...&quot;<br>}</pre><p>The problems such transformations pose are very serious in nature due to their potential for mallocing to glory. Left unchecked, the consequences are that a simple transformation step <em>e.g. repeat string n times</em> can rapidly eat up all available memory and end-up getting the program OutOfMemory killed. Worst yet, it poses a grave risk to the availability of our workflow service when this is performed at scale.</p><p>It was obvious what we needed to do given the problems at play. The solution involved having</p><ol><li>Some sort of isolation to reduce the blast radius of the transformation, therefore not affecting other workflows that may be executing in the same address space and,</li><li>Resource boundaries for the transformation code.</li></ol><p>In this three part series. I’ll explain how we have achieved the level of isolation required to protect against such transformations.</p><h3>Part 1 — Process Level Isolation</h3><p>We provide the runtime for executing workflows. The workflow service is run as a container, and it is managed by Kubernetes.</p><p>We evaluated various options including using <a href="https://aws.amazon.com/lambda/">AWS Lambda</a> (we’re an AWS shop) to execute the workflow, whilst it seemed a great fit for solving problems such as this, we have some unique operational requirements that prevented us from using Lambda. So we looked at solving this at the container level.</p><p>In order to reduce the blast radius, the untrusted transform code is run in its own address space as a separate process (instead of being run in a goroutine), and by using <a href="https://man7.org/linux/man-pages/man1/prlimit.1.html">prlimit(1)</a>, we set the maximum amount of resources (e.g. memory) that the process can use. This helps to solve both the problems — process level isolation to “sandbox” the transformation, and with the process level isolation, we have a fine grained control over the process’s resource usage.</p><p>Now that the transform code is run in its own address space, it shouldn’t matter whether to use <a href="https://github.com/itchyny/gojq">GoJq</a> or the original <a href="https://stedolan.github.io/jq/">Jq</a> (which is much more memory efficient) to perform the transformation. We chose GoJq because we can have a simple <a href="https://pkg.go.dev/net/rpc">RPC wrapper</a> that comes with Go standard library to GoJq so that the goroutine executing the workflow can simply call the transform RPC method on the jqworker process to perform the transformation — the input/output is typed, and encoding and transport is also taken care of. Although we pay a small penalty with latency due to the RPC abstraction, the inter process communication (IPC) is simplified as a result.</p><p>You can checkout the <a href="https://gist.github.com/pravinchandar/be4abb4ad0662fe58fdd203c0ce7de31">code</a> to Jqworker: it’s a simple Go program that wraps GoJq with an RPC server over a domain socket. The jqworker is compiled to a binary and run as ./jqworker &lt;sock&gt; , where &lt;sock&gt; is the unix socket where the RPC server will listen.</p><p>Using prlimit, we can set the maximum address space the jqworker program can use. In this example, we invoke the jqworker program with the address space ceiling set at 10MB. If this process were to require more than 10MB of memory at runtime, it will be SIGKILL&#39;d by the kernel.</p><pre>prlimit —-as=10485760 /path/to/jqworker /path/to.sock</pre><p>You might wonder why the cap is on the address space (--as) of the process as opposed to the RSS memory size (--rss). After all, it is actually the wired-up memory that is of interest from an OOM perspective. The reason why the “address space” is used is because the modern kernels do not have support for limiting processes based on their RSS usage. See RLIMIT_RSS in <a href="https://man7.org/linux/man-pages/man2/prlimit.2.html">prlimit(7)</a> for more info:</p><blockquote><em>RLIMIT_RSS</em></blockquote><blockquote><em>This is a limit (in bytes) on the process’s resident set (the number of virtual pages resident in RAM). This limit has effect only in Linux 2.4.x, x &lt; 30, and there affects only calls to</em><a href="https://man7.org/linux/man-pages/man2/madvise.2.html"><em> madvise(2)</em></a><em> specifying MADV_WILLNEED.</em></blockquote><p><em>I did come across a </em><a href="https://github.com/parke/rlimit_rss"><em>kernel model that enforces RLIMIT_RSS</em></a><em> in modern kernels. It’s definitely an interesting experiment, check it out!</em></p><p>But using —-as has some consequences, especially with Go binaries which you’ll see shortly.</p><p>Let’s test the jqworker binary to see if everything works, before capping the process with the prlimit.</p><p>Run jqworker as below</p><pre>$ ./jqworker /tmp/foo.sock</pre><p>We’ll use the <a href="https://gist.github.com/pravinchandar/c841feaaefff9cc015fd1018ad2160e9">test code</a> to mimic the client process that sends a simple transform request via RPC, and gets a response.</p><pre>➔ go test -run=TestJqWorker/SimpleTransform -v .<br>=== RUN   TestJqWorker<br>=== RUN   TestJqWorker/SimpleTransform<br>--- PASS: TestJqWorker (0.00s)<br>--- PASS: TestJqWorker/SimpleTransform (0.00s)<br>PASS</pre><p>This confirms everything is working as expected.</p><p>The transformations that jqworker will be performing under <em>normal conditions </em>will not consume more than a few MBs. Our p99’s run under 10MB.</p><p>We’ll run jqworker with a prlimit cap set on the address space.</p><pre>$ prlimit --as=10485760 ./jqworker /tmp/foo.sock<br>fatal error: failed to reserve page summary memory...$ prlimit --as=52428800 ./jqworker /tmp/foo.sock<br>fatal error: failed to reserve page summary memory</pre><p>Notice how the process doesn’t start even with 50MB of address space. One would think it should be more than enough for jqworker for what it does. The reason why it failed to start is because Go programs (since Go 1.14) make a <a href="https://github.com/golang/go/issues/38010#issuecomment-642654584">huge initial reservation</a> of the address space (depending on the platform) during startup. The reservation is made with the PROT_NONE flag which means the address space cannot be read, written, or executed but set aside for future use in the process. The goruntime commits to using the reserved address space progressively by marking it as read/write as and when it needs to grow the heap. The <a href="https://github.com/golang/proposal/blob/master/design/35112-scaling-the-page-allocator.md">page allocator proposal</a> contains all the juicy details about the merits of such upfront reservation, check it out.</p><p>There is a <a href="https://github.com/golang/go/issues/38010#issuecomment-642930081">workaround</a> available, and that is to compile jqworker with GOARCH=386. Since the upfront memory reservation is proportional to the address space on the target platform, and the addressable space for 386 is much smaller, we’ll be able to run jqworker with a much smaller memory footprint.</p><p>We’ll compile jqworker with GOARCH=386 and run with the prlimit cap set at 20MB.</p><pre>$ GOARCH=386 go build jqworker/main.go<br>$ prlimit --as=20971520 ./jqworker /tmp/foo.sock</pre><p>We can run the <a href="https://gist.github.com/pravinchandar/c841feaaefff9cc015fd1018ad2160e9">test code</a> again to make sure the simple transform is working as expected with prlimit.</p><pre>➔ go test -run=TestJqWorker/SimpleTransform -v .<br>=== RUN   TestJqWorker<br>=== RUN   TestJqWorker/SimpleTransform<br>--- PASS: TestJqWorker (0.00s)<br>--- PASS: TestJqWorker/SimpleTransform (0.00s)<br>PASS</pre><p>Now we can also test if the prlimit is correctly enforced by performing a transform on a 10MB data. We should expect the jqworker process to be SIGKILL’d by the kernel.</p><pre>➔ go test -run=TestJqWorker/ComplexTransform -v .<br>=== RUN TestJqWorker<br>=== RUN TestJqWorker/ComplexTransform<br> — — PASS: TestJqWorker (0.06s)<br> — — PASS: TestJqWorker/ComplexTransform (0.06s)<br>PASS</pre><p>The jqworker process, as expected, gets OOM killed because it requested for more memory than what was available to the process.</p><pre>$ prlimit --as=20971520 ./jqworker /tmp/foo.sock<br>runtime: out of memory: cannot allocate 12582912-byte block (3735552 in use)<br>fatal error: out of memory</pre><p>This is a good first step. With this approach, we’re now able to contain the transformation with process level isolation. By putting a cap on the process’s resource usage we don’t let any one process consume all available memory, therefore causing other processes running on the same server to crash.</p><p>Obviously there are overheads if we launch a process on-demand to run the transformation as we process over 10M transformations per day, there ought to be a better way to do this. In the next part, we’ll see how to have jqworkerprocesses always available so we can run transformations when we need to.</p><p><em>Coming soon: </em><strong>Part 2 — Process Manager</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=113f2756d408" width="1" height="1" alt=""><hr><p><a href="https://zendesk.engineering/running-untrusted-workload-with-go-113f2756d408">Running untrusted workload with Go</a> was originally published in <a href="https://zendesk.engineering/">Zendesk Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>