Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DC3AFC433F5 for ; Tue, 3 May 2022 18:19:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241221AbiECSW4 (ORCPT ); Tue, 3 May 2022 14:22:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241218AbiECSWy (ORCPT ); Tue, 3 May 2022 14:22:54 -0400 Received: from mail-qk1-x735.google.com (mail-qk1-x735.google.com [IPv6:2607:f8b0:4864:20::735]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB4C03EF38 for ; Tue, 3 May 2022 11:19:19 -0700 (PDT) Received: by mail-qk1-x735.google.com with SMTP id c1so14363571qkf.13 for ; Tue, 03 May 2022 11:19:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ieee.org; s=google; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to:content-transfer-encoding; bh=MKormK6iE7G36ESmhWH1ld5QzOyCAmTIaQwp9omXWzE=; b=Qz85rdHVDd2YhAj1r5Cx0L26OlVrDCVrP+0CnUwP3D8guuHYLJwqpo339itUI0dfZP 985IOO1NXzcyw/T/9WVvZXAnFGfo9brqpuMTbzJPjBUavwW3HUmr1IBlq/wNyKjEekph 3d6lwS22NOQzt/Fi0qN8INHtMkGNYccLJJUUE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=MKormK6iE7G36ESmhWH1ld5QzOyCAmTIaQwp9omXWzE=; b=DS3KRsoqn0+eDLFExbwiY3L5j8gclvo6Vi+M6N1Wp9J0AiiMdr+8IoPtGTp+gIfAoo ZDRGJ7M6L2hAMb6oHaBls9F0E2n+HpwcDnaMkcO/nWV7nF+r1CwtLr2hC8zRPjGcevur EFhgfTsWYiZh1kJ+EYFp7R0wOwpf5ICXM8hL68r47buK86DTL8IRW6XjTiusmkFgLB0B eqUgUM+Z+8NTOC89B1GlvUmZaU4W4eVPeRJCYDOKHzGiW2EzIDCVexHGwqART+FBoGep qgzGZn7QhJgd1YmRAKFXroKigqmZEgBCCvAZYy7t7umskKOFIIh1YqlT6SG8OZJOJi+/ 5LHA== X-Gm-Message-State: AOAM532VFmmLdkjzlI84C+shX4Qq3zq1vqk0Tqgg44OHn4rCdLHRuMrg x/nRNNpOK8kjJW+Dc7oi/zp2KKAeJkZoaEbt X-Google-Smtp-Source: ABdhPJwqxzOd6WojmR2DWHj462hgoHcCI1Y7x2D+DvUtJgZ/h2McWpyzld1OzshJpbt8/AzmNqIeGA== X-Received: by 2002:a05:620a:1925:b0:69e:b500:5f41 with SMTP id bj37-20020a05620a192500b0069eb5005f41mr12880806qkb.579.1651601958873; Tue, 03 May 2022 11:19:18 -0700 (PDT) Received: from [192.168.1.242] (pool-68-134-25-67.bltmmd.fios.verizon.net. [68.134.25.67]) by smtp.gmail.com with ESMTPSA id w4-20020a05620a128400b0069fc6484c06sm5736824qki.23.2022.05.03.11.19.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 03 May 2022 11:19:18 -0700 (PDT) Message-ID: <804d8b8d-394a-5070-c773-b074fe5b6a5d@ieee.org> Date: Tue, 3 May 2022 14:19:17 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.0 Subject: Re: Daemons writing into HOME_DIR Content-Language: en-US To: Stefan Schulze Frielinghaus , selinux-refpolicy@vger.kernel.org References: From: Chris PeBenito In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: selinux-refpolicy@vger.kernel.org On 5/3/22 13:01, Stefan Schulze Frielinghaus wrote: > Hi all, > > In short I'm wondering what the refpolicy way is to let a daemon write into > HOME_DIR and how those files---especially the SELinux user part---should be > labeled? > > Currently I have a daemon (systemd service) running under context > > system_u:system_r:foobar_t:s0 > > and the policy contains > > init_daemon_domain(foobar_t, foobar_exec_t) > > The daemon reads and writes files under HOME_DIR/foobar which are labeled as > foobar_rw_t and the policy has the following file context entry: > > HOME_DIR/foobar(/.*)? gen_context(system_u:object_r:foobar_rw_t,s0) > > However, newly created files still seem to have a wrong user according to > restorecon (the daemon runs under Linux user marge which is assigned to SELinux > user user_u): > > $ restorecon -FRvn /home/marge/foobar > Would relabel /home/marge/foobar/baz from system_u:object_r:foobar_rw_t:s0 to user_u:object_r:foobar_rw_t:s0 > > It looks like as if user_u wins over system_u for files under HOME_DIR. This > does not have any effect on the functionality of the daemon, however, it still > feels wrong to me. This is genhomedircon setting the seuser of the files to match the seuser mapping in `semanage login`. You want this behavior, especially if you have UBAC turned on, otherwise UBAC doesn't provide a benefit, since system_u is excluded from UBAC. > So I'm wondering how to fix this and thought about: > > 1) Can/Should a daemon run under a different SELinux user than system_u? If this is a system daemon, e.g. started by systemd (pid 1) then that is not expected in refpolicy, not generally suggested. If this is a daemon running out of a user session, such as systemd --user, then yes, it should have the user's seuser, e.g. user_u. > 2) Another option, which I think is worse, would be to the change the SELinux > user from user_u to system_u for Linux user marge under which the daemon runs. Running an interactive user as system_u is contrary to system_u's purpose, which is for non-interactive system processes only. > 3) A third option would be to keep the users as is, i.e., let the daemon run > under system_u and let marge be assigned to user_u, but tweak the policy to keep > the file context labels under HOME_DIR with system_u. See my first comment. > Any thoughts? You could change the default_user[1] so the seuser comes from the parent directory, but that would change it for the entire system which may have unintended and worse consequences. You're seeing the behavior I expect to see for this type of policy design. [1] https://github.com/SELinuxProject/selinux-notebook/blob/main/src/default_rules.md#default_user -- Chris PeBenito