Something filters out TMPDIR, even for classic snaps

As reported here https://bugs.launchpad.net/snapd/+bug/1682308:

After snap install --classic go:

$ cat tmpdir.go
package main

import (
 "fmt"
 "os"
)

func main() {
 fmt.Printf("%q\n", os.Getenv("TMPDIR"))
}
$ TMPDIR=/tmp go run tmpdir.go
"/tmp"
$ TMPDIR=/tmp /snap/bin/go run tmpdir.go
""

This is surprising.

I can see that there is code in snap-confine for setting TMPDIR, but that doesn’t run (afaict) for a classic snap, and anyway certainly doesn’t unset TMPDIR, rather the opposite…

So we found that the dynamic linked does unset TMPDIR (but not TEMPDIR) for setuid root executables… We also found a small bug in snap-confine where those two would only be set once (this is now https://github.com/snapcore/snapd/pull/3188).

We could use a hack where snap-run would save those in, say, SNAPD_TMPDIR and snap-confine would restore TMPDIR to $SNAPD_TMPDIR but I’m not sure if this is worth the effort.