Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remediation for BTM corruption if users are upgrading from 1.1.12.81501 #519

Closed
wants to merge 7 commits into from

Conversation

rickheil
Copy link

@rickheil rickheil commented Oct 6, 2023

Preinstall: checks if a problematic version (81501) is installed, and touches a file if so.
Postinstalls: same logic in both, looks for the touch file and if found runs the remediation.

Currently getting an "illegal byte sequence" error as I wrote this assuming /bin/sh and not ZSH - will continue working on this in Slack after dinner.

All the credit to @kevinmcox and @tuxudo for finding the BTM plist is editable and figuring out the PlistBuddy pieces.

/bin/sh behaved differently, likely due to LANG being set to
something very basic. ZSH on the other hand was throwing
errors about "illegal byte sequence." Forcing a basic encoding
seems to get everything parsed properly.
Creates a new postinstall for the app-only pkg. This will run
remediation of the 81501 issues for shops that use the separate
launchagent and app packages.
Modify the build_nudge.zsh to copy the post
@rickheil rickheil changed the title WIP: Add remediation for BTM corruption if users are upgrading from 1.1.12.81501 Add remediation for BTM corruption if users are upgrading from 1.1.12.81501 Oct 7, 2023
@rickheil
Copy link
Author

rickheil commented Oct 7, 2023

Ready for testing!

@rickheil-samsara
Copy link

For linking purposes: fixes #515

@kevinmcox
Copy link
Contributor

Thanks for putting this all together @rickheil!

@erikng
Copy link
Member

erikng commented Oct 7, 2023

I have a theory. What if we ran the nudge uninstall SMApp cli flag instead? Would it remove the same keys from that file?

@erikng
Copy link
Member

erikng commented Oct 7, 2023

Also you need to do this on the dev branch not main. I haven't merged the other code yet into main.

@kevinmcox
Copy link
Contributor

Erik, is that the --unregister flag?

I tried that one yesterday and it didn't work.

That said, I can't promise I tried it in all combinations of scenarios, so definitely worth trying again after installing 1.1.13.

Copy link
Member

@natewalck natewalck left a comment

Choose a reason for hiding this comment

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

Looking pretty good.

remediate=True
echo "Set BTM plist path to v4"
else
echo "Error: unable to locate a BTM plist, no remediation occurred. Does this OS support BTM features?"
Copy link
Member

Choose a reason for hiding this comment

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

If we are doing nothing else in the postinstall, we should exit here.

signing_re='.*"com.github.macadmins.Nudge".*T4SK8ZXCXG\)'
dne_re=", Does Not Exist"

while $remediate; do
Copy link
Member

Choose a reason for hiding this comment

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

Since we are exiting early above, we can get rid of the while altogether and flatten this to a if/elif/else statement.

done

# Remove the touch file to not trigger remediation again.
rm /private/var/tmp/nudge_remediate
Copy link
Member

Choose a reason for hiding this comment

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

If we flatten this, the only negative is we may need to rm this above around line 32 as well. Flattening the loops seems worth it for readability.

# Determine BTM plist path. On macOS 13.3 and lower, v4; higher and on to 14, v8.
if [[ -f "/private/var/db/com.apple.backgroundtaskmanagement/BackgroundItems-v8.btm" ]]; then
btm_plist_path="/private/var/db/com.apple.backgroundtaskmanagement/BackgroundItems-v8.btm"
remediate=True
Copy link
Member

Choose a reason for hiding this comment

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

Come to think of it, if we are exiting on the else around line 31, we don't need to set this variable at all.

@erikng
Copy link
Member

erikng commented Oct 9, 2023

I have a v3 file on disk on my machine, so that implies that the v4 and lower logic is not a correct assumption.

@tuxudo
Copy link

tuxudo commented Oct 9, 2023

Do you have a v4 or v8 on disk? It would appear the v3 is from a very early version of Ventura

@erikng
Copy link
Member

erikng commented Oct 9, 2023

I no longer think this option is viable and am tempted to close the PR. This change is going to require full disk access.

When I first saw the PR, I thought it was a security issue in that if you can delete, you can add. In my testing, accessing this file on a terminal without FDA gives this error.

/private/var/db/com.apple.backgroundtaskmanagement/BackgroundItems-v8.btm: file does not exist or is not readable or is not a regular file (Error Domain=NSCocoaErrorDomain Code=257 "The file "BackgroundItems-v8.btm" couldn't be opened because you don't have permission to view it." UserInfo={NSFilePath=/private/var/db/com.apple.backgroundtaskmanagement/BackgroundItems-v8.btm, NSUnderlyingError=0x600003548000 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}})

^ error is running as the root user as well.

@natewalck
Copy link
Member

Thats great then. Apple's edge case here is going to cause a bit of...pain.

@erikng erikng closed this Oct 9, 2023
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.

6 participants