From patchwork Tue Jun 8 18:30:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Xinglu Chen X-Patchwork-Id: 30034 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 0551B27BC81; Tue, 8 Jun 2021 19:31:13 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS, T_DKIM_INVALID,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 10B6127BC78 for ; Tue, 8 Jun 2021 19:31:12 +0100 (BST) Received: from localhost ([::1]:37130 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lqgVL-00037H-0g for patchwork@mira.cbaines.net; Tue, 08 Jun 2021 14:31:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:60446) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lqgVD-00033f-6W for guix-patches@gnu.org; Tue, 08 Jun 2021 14:31:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:48237) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lqgVC-0002gh-OI for guix-patches@gnu.org; Tue, 08 Jun 2021 14:31:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lqgVC-0002s0-MF for guix-patches@gnu.org; Tue, 08 Jun 2021 14:31:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#48923] [PATCH v2] activation: Add =?utf-8?q?=E2=80=98call-with?= =?utf-8?q?-output-file*=E2=80=99?= procedure. Resent-From: Xinglu Chen Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 08 Jun 2021 18:31:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 48923 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 48923@debbugs.gnu.org Cc: Maxime Devos Received: via spool by 48923-submit@debbugs.gnu.org id=B48923.162317702710969 (code B ref 48923); Tue, 08 Jun 2021 18:31:02 +0000 Received: (at 48923) by debbugs.gnu.org; 8 Jun 2021 18:30:27 +0000 Received: from localhost ([127.0.0.1]:59781 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lqgUc-0002qq-NP for submit@debbugs.gnu.org; Tue, 08 Jun 2021 14:30:27 -0400 Received: from h87-96-130-155.cust.a3fiber.se ([87.96.130.155]:37574 helo=mail.yoctocell.xyz) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lqgUY-0002qY-Bb for 48923@debbugs.gnu.org; Tue, 08 Jun 2021 14:30:25 -0400 From: Xinglu Chen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=yoctocell.xyz; s=mail; t=1623177014; bh=915n3rghIFKNR72xC75ghfOJjwcE+tnDEMvdJxYZvNk=; h=From:To:Cc:Subject:In-Reply-To:References:Date; b=QCWscX/i4wUnCfgCt62AARjsM84PrAXemOJonkG+/fYvoHOxqe4DVc8BnpFACsJm3 1Fay8hISSKJjt1po3ZocpdQZDzxden19HiTneBpx9/C5+tr+9aJXeILX/m7+KlV7Z9 DtkCsL79/+QObsEh7BtQijZap6AxYBckSmFM4zKY= In-Reply-To: <23ac66d29119c5395fee0e993ea0fe811beefd91.1623166798.git.public@yoctocell.xyz> References: <23ac66d29119c5395fee0e993ea0fe811beefd91.1623166798.git.public@yoctocell.xyz> Message-Id: <7500e1ba2d55d397d4c105ca189746f78de02d35.1623176839.git.public@yoctocell.xyz> Date: Tue, 08 Jun 2021 20:30:13 +0200 MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches Using ‘call-with-output-file*’ instead of ‘call-with-output-file’ and ‘chmod’ will prevent secrets from being leaked. See . * guix/build/activation.scm (call-with-output-file*): New procedure. * doc/guix.texi (Activation): New section; document ‘call-with-output-file*’. --- Changes since v1: * Moved ‘call-with-output-file*’ from (gnu build utils) to (gnu build activation). * Added a “Activation” section in the manual to document the new procedure. doc/guix.texi | 31 +++++++++++++++++++++++++++++++ gnu/build/activation.scm | 13 ++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) base-commit: 503c2039a280dd52a751a6852b4157fccd1b4195 diff --git a/doc/guix.texi b/doc/guix.texi index 59b4ac11b4..643c7ff126 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -321,6 +321,7 @@ System Configuration * Invoking guix deploy:: Deploying a system configuration to a remote host. * Running Guix in a VM:: How to run Guix System in a virtual machine. * Defining Services:: Adding new service definitions. +* Activation:: Setting up system-wide files and directories. Services @@ -13386,6 +13387,7 @@ instance to support new system services. * Invoking guix deploy:: Deploying a system configuration to a remote host. * Running Guix in a VM:: How to run Guix System in a virtual machine. * Defining Services:: Adding new service definitions. +* Activation:: Setting up system-wide files and directories. @end menu @node Using the Configuration System @@ -34633,6 +34635,35 @@ system: This service represents PID@tie{}1. @end defvr +@node Activation +@section Activation + +@dfn{Activation} is the process that sets up system-wide files and +directories so that an @code{operating-system} (@pxref{operating-system +Reference}) configuration becomes active. This will happen when +invoking commands like @command{guix system reconfigure} or +@command{guix system switch-generation}, but not when invoking +@command{guix system build} (@pxref{Invoking guix system}). + +@deffn {Scheme Procedure} call-with-output-file* @var{file} @var{proc} @ + [#:perms #o666] +Open FILE for output, set the file permission bits to @var{perms}, and +call @code{(PROC port)} with the resulting port. + +The advantage of using this procedure compared to something like this + +@lisp +(call-with-output-file "FILE" + (lambda (port) + (display "top secret" port))) +(chmod "FILE" #o400) +@end lisp + +is that, with the latter, an unpriviliged user could open @var{file} +before the permission was changed to @code{#o400}, thus making it +possible to leak sensitive information. +@end deffn + @node Documentation @chapter Documentation diff --git a/gnu/build/activation.scm b/gnu/build/activation.scm index 2af1d44b5f..0054079cb6 100644 --- a/gnu/build/activation.scm +++ b/gnu/build/activation.scm @@ -6,6 +6,7 @@ ;;; Copyright © 2018 Arun Isaac ;;; Copyright © 2018, 2019 Ricardo Wurmus ;;; Copyright © 2021 Maxime Devos +;;; Copyright © 2021 Xinglu Chen ;;; ;;; This file is part of GNU Guix. ;;; @@ -34,6 +35,7 @@ #:use-module (srfi srfi-1) #:use-module (srfi srfi-11) #:use-module (srfi srfi-26) + #:use-module (srfi srfi-60) #:export (activate-users+groups activate-user-home activate-etc @@ -43,7 +45,8 @@ activate-firmware activate-ptrace-attach activate-current-system - mkdir-p/perms)) + mkdir-p/perms + call-with-output-file*)) ;;; Commentary: ;;; @@ -102,6 +105,14 @@ Warning: this is currently suspect to a TOCTTOU race!" (chown directory (passwd:uid owner) (passwd:gid owner)) (chmod directory bits)) +;; Prevent secrets from leaking, see +(define* (call-with-output-file* file proc #:key (perms #o666)) + "FILE should be string containg the path to a file, PROC should be a procedure +that accepts the port as an argument, and PERMS should be the permission bits +of the file, the default is 666." + (let ((port (open file (bitwise-ior O_WRONLY O_CREAT) perms))) + (call-with-port port proc))) + (define* (copy-account-skeletons home #:key (directory %skeleton-directory)