1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#pragma once
///@file

#include "lix/libstore/build/derivation-goal.hh"
#include "lix/libstore/build/request.capnp.h"
#include "lix/libstore/local-store.hh"
#include "lix/libutil/error.hh"
#include "lix/libutil/processes.hh"
#include "lix/libutil/cgroup.hh"
#include <capnp/message.h>

namespace nix {

struct BuildContext
{
#ifdef __linux__
    /**
     * Control group for this derivation goal.
     */
    std::optional<AutoDestroyCgroup> cgroup;
#endif
};

struct LocalDerivationGoal : public DerivationGoal
{
    LocalStore & getLocalStore();

    /**
     * User selected for running the builder.
     */
    std::unique_ptr<UserLock> buildUser;

    /**
     * Build context for this goal.
     */
    BuildContext context;

    /**
     * The process group of the builder, or its exit status.
     */
    std::variant<ProcessGroup, int> pg;

    /**
     * The temporary directory.
     */
    Path tmpDirRoot, tmpDir;

    /**
     * The temporary directory file descriptor
     */
    AutoCloseFD tmpDirFd;

    /**
     * The path of the temporary directory in the sandbox.
     */
    Path tmpDirInSandbox;

    /**
     * Master side of the pseudoterminal used for the builder's
     * standard output/error.
     */
    AutoCloseFD builderOutPTY;

    /**
     * Whether we're currently doing a chroot build.
     */
    bool useChroot = false;

    Path chrootRootDir;

    /**
     * RAII object to delete the chroot directory.
     */
    std::shared_ptr<AutoDelete> autoDelChroot;

    /**
     * Stuff we need to pass to initChild().
     */
    struct ChrootPath {
        Path source;
        bool optional;
        ChrootPath(Path source = "", bool optional = false)
            : source(source), optional(optional)
        { }
    };
    typedef map<Path, ChrootPath> PathsInChroot; // maps target path to source path
    PathsInChroot pathsInChroot;

    typedef map<std::string, std::string> Environment;
    Environment env;

#if __APPLE__
    typedef std::string SandboxProfile;
    SandboxProfile additionalSandboxProfile;
#endif

    /**
     * Hash rewriting.
     */
    StringMap inputRewrites, outputRewrites;
    typedef map<StorePath, StorePath> RedirectedOutputs;
    RedirectedOutputs redirectedOutputs;

    /**
     * The outputs paths used during the build.
     *
     * - Input-addressed derivations or fixed content-addressed outputs are
     *   sometimes built when some of their outputs already exist, and can not
     *   be hidden via sandboxing. We use temporary locations instead and
     *   rewrite after the build. Otherwise the regular predetermined paths are
     *   put here.
     *
     * - Floating content-addressed derivations do not know their final build
     *   output paths until the outputs are hashed, so random locations are
     *   used, and then renamed. The randomness helps guard against hidden
     *   self-references.
     */
    OutputPathMap scratchOutputs;

    /**
     * Path registration info from the previous round, if we're
     * building multiple times. Since this contains the hash, it
     * allows us to compare whether two rounds produced the same
     * result.
     */
    std::map<Path, ValidPathInfo> prevInfos;

    uid_t sandboxUid();
    gid_t sandboxGid();

    const static Path homeDir;

    /**
     * Create a LocalDerivationGoal without an on-disk .drv file,
     * possibly a platform-specific subclass
     */
    static std::unique_ptr<LocalDerivationGoal> makeLocalDerivationGoal(
        const StorePath & drvPath,
        const OutputsSpec & wantedOutputs,
        Worker & worker,
        bool isDependency,
        BuildMode buildMode
    );

    /**
     * Create a LocalDerivationGoal for an on-disk .drv file,
     * possibly a platform-specific subclass
     */
    static std::unique_ptr<LocalDerivationGoal> makeLocalDerivationGoal(
        DrvHasRoot drvRoot,
        const StorePath & drvPath,
        const BasicDerivation & drv,
        const OutputsSpec & wantedOutputs,
        Worker & worker,
        bool isDependency,
        BuildMode buildMode
    );

    virtual ~LocalDerivationGoal() noexcept(false) override;

    /**
     * Whether we need to perform hash rewriting if there are valid output paths.
     */
   virtual bool needsHashRewrite();

    /**
     * The additional states.
     */
    kj::Promise<Result<WorkResult>> tryLocalBuild() noexcept override;

    /**
     * Start building a derivation.
     */
    kj::Promise<Result<void>> startBuilder();

    /**
     * Fill in the environment for the builder.
     */
    void initEnv();

    /**
     * Setup tmp dir location.
     */
    void initTmpDir();

    /**
     * Setup the configured certificate authority for the builder.
     */
    void setupConfiguredCertificateAuthority();

    /**
     * Write a JSON file containing the derivation attributes.
     */
    kj::Promise<Result<void>> writeStructuredAttrs();

    /**
     * Make a file owned by the builder addressed by its path.
     *
     * SAFETY: this function is prone to TOCTOU as it receives a path and not a descriptor.
     * It's only safe to call in a child of a directory only visible to the owner.
     */
    void chownToBuilder(const Path & path);

    /**
     * Make a file owned by the builder addressed by its file descriptor.
     */
    void chownToBuilder(const AutoCloseFD & fd);

    int getChildStatus() override;

    /**
     * Check that the derivation outputs all exist and register them
     * as valid.
     */
    kj::Promise<Result<SingleDrvOutputs>> registerOutputs() override;

    /**
     * Check that an output meets the requirements specified by the
     * 'outputChecks' attribute (or the legacy
     * '{allowed,disallowed}{References,Requisites}' attributes).
     */
    kj::Promise<Result<void>> checkOutputs(const std::map<std::string, ValidPathInfo> & outputs, const std::map<std::string, StorePath> & alreadyRegisteredOutputs);

    /**
     * Close the read side of the logger pipe.
     */
    void closeReadPipes() override;

    /**
     * Cleanup hooks for buildDone()
     */
    void cleanupHookFinally() override;
    void cleanupPreChildKill() override;
    void cleanupPostChildKill() override;
    bool cleanupDecideWhetherDiskFull() override;
    void cleanupPostOutputsRegisteredModeCheck() override;
    void cleanupPostOutputsRegisteredModeNonCheck() override;

    /**
     * Delete the temporary directory or make it visible to the user requesting
     * this build, if a temporary directory was created at all. Temporary files
     * of derivations using builtin builders are deleted even for `keep-failed`
     * builds as otherwise we may expose secrets (e.g. from the system .netrc).
     */
    void finalizeTmpDir(bool force, bool duringDestruction = false);

    /**
     * Forcibly kill the child process, if any.
     *
     * Called by destructor, can't be overridden
     */
    void killChild() override final;

    /**
     * Kill any processes running under the build user UID.
     */
    virtual void killSandbox(bool getStats);

    /**
     * Create alternative path calculated from but distinct from the
     * input, so we can avoid overwriting outputs (or other store paths)
     * that already exist.
     */
    StorePath makeFallbackPath(const StorePath & path);

    /**
     * Make a path to another based on the output name along with the
     * derivation hash.
     *
     * @todo Add option to randomize, so we can audit whether our
     * rewrites caught everything
     */
    StorePath makeFallbackPath(OutputNameView outputName);

protected:
    using DerivationGoal::DerivationGoal;

    /**
     * Setup dependencies outside the sandbox.
     * Called in the parent nix process.
     */
    virtual void prepareSandbox()
    {
        throw Error("sandboxing builds is not supported on this platform");
    };

    /**
     * Create a new process that runs `openSlave` and `runChild`
     * On some platforms this process is created with sandboxing flags.
     */
    virtual Pid startChild(AutoCloseFD setupFD, AutoCloseFD logPTY);

    kj::Promise<Result<WorkResult>> handleRawChild() noexcept;
    kj::Promise<Result<std::optional<WorkResult>>> handleRawChildStream() noexcept;

    virtual void fillBuilderConfig(build::Request::Builder request) {}

    /**
     * Create a special accessor that can access paths that were built within the sandbox's
     * chroot.
     */
    virtual std::optional<ref<FSAccessor>> getChrootDirAwareFSAccessor()
    {
        return std::nullopt;
    };

    /**
     * Whether derivation can be built on current platform with `uid-range` feature
     */
    virtual bool supportsUidRange()
    {
        return false;
    }

    virtual bool respectsTimeouts() override
    {
        return true;
    }
};

}