Skip to content

fix(installer): normalise tree ownership before re-run update (unblocks re-runs after a partial install) - #1839

Merged
jaylfc merged 1 commit into
devfrom
fix/installer-rerun-ownership
Jul 16, 2026
Merged

fix(installer): normalise tree ownership before re-run update (unblocks re-runs after a partial install)#1839
jaylfc merged 1 commit into
devfrom
fix/installer-rerun-ownership

Conversation

@jaylfc

@jaylfc jaylfc commented Jul 16, 2026

Copy link
Copy Markdown
Owner

Problem

Reported on #2 by a community tester on a fresh Orange Pi 5 Plus (Armbian trixie). A re-run of install-server.sh over an existing checkout dies with:

[server-install] updating existing checkout
error: unable to unlink old 'docs/agent-manual/04-apps.md': Permission denied
...
fatal: Could not reset index file to revision 'origin/master'.

Root cause

The update path deliberately drops to the repo-owning user (taos) for the git fetch + reset --hard, so git never runs as root inside a user-writable tree (a real privilege-escalation guard). But it decides the owner by stat-ing only the top-level $INSTALL_DIR.

If a prior install was interrupted mid-chown (or any root step wrote a few paths back as root), the tree ends up with mixed ownership. The owning user then cannot unlink the still-root-owned paths, so reset --hard fails and every subsequent re-run is bricked. This is easy to hit: first run hiccups (tight disk, incus init, Ctrl-C), second run cannot recover.

Fix

Normalise ownership to the owning user with chown -R (performed by root) immediately before the update, so the reset can rewrite the whole tree. git still runs unprivileged, so the security guard the original code added is preserved.

Validation

  • bash -n clean.
  • Failure class reproduced locally: an unwritable path in the tree produces the identical unable to unlink ...: Permission denied; normalising the tree makes reset --hard apply cleanly (correctly removing deleted paths).

Summary by CodeRabbit

  • Bug Fixes
    • Improved server re-installation and update reliability when installation files have mixed ownership.
    • Prevented update failures caused by leftover root-owned files from previous installations.

A re-run of install-server.sh over an existing checkout drops to the
repo-owning user (the 'taos' service user) for the git fetch + reset, to
avoid running git as root inside a user-writable tree. But it only reads
the TOP-LEVEL dir owner. If a prior install was interrupted mid-chown (or
a root step wrote a few paths back), the tree has MIXED ownership: the
owning user then cannot unlink the still-root-owned paths, so the reset
fails with 'unable to unlink old ...: Permission denied' ->
'Could not reset index file to revision origin/master', bricking every
subsequent re-run.

Normalise ownership to the owning user (chown -R, run by root) right
before the update so the reset can rewrite the whole tree. Safe: root
does the chown and git still runs unprivileged.

Reported on #2 (fresh Orange Pi 5 Plus, retry after a partial first run).
Failure class reproduced locally: an unwritable path in the tree yields
the identical unlink-EACCES; normalising the tree makes the reset apply
cleanly.
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The installer’s root re-run path now recursively changes ownership of a non-root-owned checkout to its detected owner before running git fetch and git reset --hard.

Changes

Installer ownership handling

Layer / File(s) Summary
Normalize checkout ownership before Git update
scripts/install-server.sh
Root reruns now recursively assign the existing install directory to its detected owner when ownership differs, preventing mixed ownership during subsequent Git updates.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • jaylfc/taOS#724: Updates install-directory ownership and permissions in the same installer flow.
  • jaylfc/taOS#754: Handles ownership and permission repair during installer reruns.
  • jaylfc/taOS#768: Handles repository ownership mismatches in the existing-checkout update path.

Suggested reviewers: hognek

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: normalizing ownership before rerun updates to fix partial-install re-runs.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/installer-rerun-ownership

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gitar-bot

gitar-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown

Important

You are using the Gitar free plan. Upgrade to unlock code review, CI analysis, auto-apply, custom automations, and more.

Gitar

Comment thread scripts/install-server.sh
# "Could not reset index file"), bricking every re-run. Normalise
# ownership to the owning user first so the reset can rewrite the whole
# tree. Safe: root performs the chown and git still runs unprivileged.
chown -R "$_repo_owner" "$INSTALL_DIR"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: chown -R dereferences symlinks by default (GNU chown without -h/--no-dereference).

The surrounding code is explicitly defending against a hostile-writable tree (git's dubious-ownership / privilege-escalation guard). If an attacker can plant a symlink inside $INSTALL_DIR, a root-run chown -R will follow it and walk outside the tree, handing ownership of arbitrary files to the unprivileged $_repo_owner. That partially undermines the very isolation this block is trying to preserve. Use chown -Rh "$_repo_owner" "$INSTALL_DIR" (or --no-dereference) so symlink targets are not traversed.

Suggested change
chown -R "$_repo_owner" "$INSTALL_DIR"
chown -Rh "$_repo_owner" "$INSTALL_DIR"

Reply with @kilocode-bot fix it to have Kilo Code address this issue.

Comment thread scripts/install-server.sh
# "Could not reset index file"), bricking every re-run. Normalise
# ownership to the owning user first so the reset can rewrite the whole
# tree. Safe: root performs the chown and git still runs unprivileged.
chown -R "$_repo_owner" "$INSTALL_DIR"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: This chown -R runs unguarded under set -euo pipefail (set at line 35). If it fails for any reason — a path vanished mid-run, EPERM on an immutable/locked file, or chown hitting a mount it can't traverse — the entire install aborts before the git fetch/reset even runs. Since this is a best-effort normalisation step added to recover from a degraded state, consider guarding it (e.g. chown -Rh ... || warn "ownership normalisation incomplete; continuing") so a chown hiccup doesn't turn a recoverable re-run into a hard failure.


Reply with @kilocode-bot fix it to have Kilo Code address this issue.

@kilo-code-bot

kilo-code-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown

Code Review Summary

Status: 2 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 1
Issue Details (click to expand)

WARNING

File Line Issue
scripts/install-server.sh 1173 chown -R dereferences symlinks by default; a planted symlink in the tree could make the root-run chown walk outside $INSTALL_DIR, undoing the isolation this block is meant to preserve. Use chown -Rh / --no-dereference.

SUGGESTION

File Line Issue
scripts/install-server.sh 1173 chown -R is unguarded under set -euo pipefail (line 35); a chown failure aborts the entire install before the git update runs. Consider guarding it so a hiccup doesn't turn a recoverable re-run into a hard failure.
Files Reviewed (1 files)
  • scripts/install-server.sh - 2 issues

Fix these issues in Kilo Cloud


Reviewed by hy3:free · Input: 36.9K · Output: 2.6K · Cached: 118.8K

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
scripts/install-server.sh (1)

1173-1173: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick win

Normalize group ownership and prevent option injection.

Consider appending a colon (:) to $_repo_owner (assuming it holds only the username). This instructs chown to also normalize the group to the user's primary login group, cleaning up any residual root group ownership from the interrupted install, which could otherwise cause inconsistent permissions or setgid inheritance issues.

Additionally, use -- to prevent path option injection if $INSTALL_DIR ever begins with a hyphen.

🛠️ Proposed refactor
-        chown -R "$_repo_owner" "$INSTALL_DIR"
+        chown -R -- "$_repo_owner:" "$INSTALL_DIR"

Security Note (Privilege Escalation via Hardlinks):
The inline comment notes that running chown as root is safe here, but keep in mind that executing chown -R as root on a directory owned by an unprivileged user introduces a classic hardlink attack vector. If the underlying OS does not enforce hardlink protections (i.e., fs.protected_hardlinks=0), a malicious user could hardlink a sensitive file (e.g., /etc/shadow) into $INSTALL_DIR. The recursive chown would then inadvertently transfer ownership of the original file to $_repo_owner. Since modern Linux kernels enable protected_hardlinks=1 by default, this risk is mitigated on most contemporary systems, but it remains an architectural consideration if this installer supports legacy or custom OS environments.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@scripts/install-server.sh` at line 1173, Update the recursive ownership
command around chown to use "$_repo_owner:" so the repository owner’s primary
group is normalized, and add "--" before "$INSTALL_DIR" to prevent option
injection. Preserve the existing recursive ownership behavior and do not broaden
the change to unrelated hardlink protections.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@scripts/install-server.sh`:
- Line 1173: Update the recursive ownership command around chown to use
"$_repo_owner:" so the repository owner’s primary group is normalized, and add
"--" before "$INSTALL_DIR" to prevent option injection. Preserve the existing
recursive ownership behavior and do not broaden the change to unrelated hardlink
protections.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: edccaa4b-f415-4a8b-bdcb-dd6128e7aaa0

📥 Commits

Reviewing files that changed from the base of the PR and between 063004f and 69d463b.

📒 Files selected for processing (1)
  • scripts/install-server.sh

@jaylfc
jaylfc merged commit 5d5dec3 into dev Jul 16, 2026
11 checks passed
@jaylfc
jaylfc deleted the fix/installer-rerun-ownership branch July 16, 2026 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant