Skip to content

Commit 86b4a6e

Browse files
committed
Perms attribute can now override immutable bit
The perms attribute of the files promise can now override the immutable bit. Ticket: ENT-10961, CFE-1840 Changelog: Commit Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
1 parent 1363022 commit 86b4a6e

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

cf-agent/verify_files_utils.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,7 +2320,8 @@ static PromiseResult VerifyName(EvalContext *ctx, char *path, const struct stat
23202320

23212321
if (MakingChanges(ctx, pp, attr, &result, "rename file '%s' to '%s'", path, newname))
23222322
{
2323-
if (safe_chmod(changes_path, newperm) == 0)
2323+
const bool override_immutable = EvalContextOverrideImmutableGet(ctx);
2324+
if (OverrideImmutableChmod(changes_path, newperm, override_immutable))
23242325
{
23252326
RecordChange(ctx, pp, attr, "Changed permissions of '%s' to 'mode %04jo'",
23262327
path, (uintmax_t)newperm);
@@ -2335,7 +2336,6 @@ static PromiseResult VerifyName(EvalContext *ctx, char *path, const struct stat
23352336

23362337
if (!FileInRepository(newname))
23372338
{
2338-
const bool override_immutable = EvalContextOverrideImmutableGet(ctx);
23392339
if (!OverrideImmutableRename(changes_path, changes_newname, override_immutable))
23402340
{
23412341
RecordFailure(ctx, pp, attr, "Error occurred while renaming '%s'", path);
@@ -2653,7 +2653,8 @@ static PromiseResult VerifyFileAttributes(EvalContext *ctx, const char *file, co
26532653
if (MakingChanges(ctx, pp, attr, &result, "change permissions of '%s' from %04jo to %04jo",
26542654
file, (uintmax_t)dstat->st_mode & 07777, (uintmax_t)newperm & 07777))
26552655
{
2656-
if (safe_chmod(changes_file, newperm & 07777) == -1)
2656+
const bool override_immutable = EvalContextOverrideImmutableGet(ctx);
2657+
if (!OverrideImmutableChmod(changes_file, newperm & 07777, override_immutable))
26572658
{
26582659
RecordFailure(ctx, pp, attr, "Failed to change permissions of '%s'. (chmod: %s)",
26592660
file, GetErrorStr());

libpromises/override_fsattrs.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <files_copy.h>
66
#include <cf3.defs.h>
77
#include <string_lib.h>
8+
#include "file_lib.h"
89

910
bool OverrideImmutableBegin(
1011
const char *orig, char *copy, size_t copy_len, bool override)
@@ -162,6 +163,28 @@ void ResetTemporarilyClearedImmutableBit(
162163
}
163164
}
164165

166+
bool OverrideImmutableChmod(const char *filename, mode_t mode, bool override)
167+
{
168+
assert(filename != NULL);
169+
170+
bool is_immutable;
171+
FSAttrsResult res =
172+
TemporarilyClearImmutableBit(filename, override, &is_immutable);
173+
174+
int ret = safe_chmod(filename, mode);
175+
if (ret == -1)
176+
{
177+
Log(LOG_LEVEL_ERR,
178+
"Failed to change mode on file '%s': %s",
179+
filename,
180+
GetErrorStr());
181+
}
182+
183+
ResetTemporarilyClearedImmutableBit(filename, override, res, is_immutable);
184+
185+
return ret == 0;
186+
}
187+
165188
bool OverrideImmutableRename(
166189
const char *old_filename, const char *new_filename, bool override)
167190
{

libpromises/override_fsattrs.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <stdbool.h>
3030
#include <stddef.h>
3131
#include <utime.h>
32+
#include <sys/types.h>
3233

3334
/**
3435
* @brief Creates a mutable copy of the original file
@@ -55,6 +56,16 @@ bool OverrideImmutableBegin(
5556
bool OverrideImmutableCommit(
5657
const char *orig, const char *copy, bool override, bool abort);
5758

59+
/**
60+
* @brief Change mode on an immutable file
61+
* @param filename Name of the file
62+
* @param mode The file mode
63+
* @param override Whether to actually do override
64+
* @return false in case of failure
65+
* @note It uses safe_chmod() under the hood
66+
*/
67+
bool OverrideImmutableChmod(const char *filename, mode_t mode, bool override);
68+
5869
/**
5970
* @brief Temporarily clears the immutable bit of the old file and renames the
6071
* new to the old

0 commit comments

Comments
 (0)