Skip to content

Instantly share code, notes, and snippets.

@reubenmiller
Created February 17, 2023 15:54
Show Gist options
  • Select an option

  • Save reubenmiller/94f8abac5457f2b0bd88c7b5bf4bef91 to your computer and use it in GitHub Desktop.

Select an option

Save reubenmiller/94f8abac5457f2b0bd88c7b5bf4bef91 to your computer and use it in GitHub Desktop.
Test cases to check tedge locking
#!/bin/sh
cleanup() {
sudo systemctl stop tedge-agent || true
sudo rm -f /run/lock/tedge-agent.lock
}
test_case_1() {
echo
echo "--------------------------------------------------------"
echo "Test case 1: sticky flag OFF, with chmod 666"
echo "--------------------------------------------------------"
cleanup
chmod o-t /run/lock
sudo -u tedge timeout 3 tedge-agent
sudo chmod 666 /run/lock/tedge-agent.lock
sudo timeout 3 tedge-agent
}
test_case_2() {
echo
echo "--------------------------------------------------------"
echo "Test case 2: sticky flag OFF, without chmod 666"
echo "--------------------------------------------------------"
cleanup
chmod o-t /run/lock
sudo -u tedge timeout 3 tedge-agent
sudo timeout 3 tedge-agent
}
#
# Failing test cases
#
test_case_3() {
echo
echo "--------------------------------------------------------"
echo "Test case 3: stick flag ON, chmod 666"
echo "--------------------------------------------------------"
cleanup
chmod o+t /run/lock
sudo -u tedge timeout 3 tedge-agent
sudo chmod 666 /run/lock/tedge-agent.lock
sudo timeout 3 tedge-agent
}
test_case_4() {
echo
echo "--------------------------------------------------------"
echo "Test case 4: stick flag ON"
echo "--------------------------------------------------------"
cleanup
# Will fail
chmod o+t /run/lock
sudo -u tedge timeout 3 tedge-agent
sudo timeout 3 tedge-agent
}
test_case_5() {
echo
echo "--------------------------------------------------------"
echo "Test case 5: stick flag ON, but try to turn off on single file"
echo "--------------------------------------------------------"
cleanup
chmod o+t /run/lock
chmod o-t /run/lock/tedge-agent.lock
sudo -u tedge timeout 3 tedge-agent
sudo timeout 3 tedge-agent
}
test_case_6() {
echo
echo "--------------------------------------------------------"
echo "Test case 6: Use subfolder with sticky flag OFF"
echo "--------------------------------------------------------"
# cleanup
sudo rm -rf /run/lock/tedge
sudo chmod o+t /run/lock
echo "Create subfolder with chmod 777 and without sticky"
sudo mkdir -p /run/lock/tedge
sudo chmod 777 /run/lock/tedge
# Technically this is not needed, but used to ensure that the
# subfolder definitely does not
sudo chmod o-t /run/lock/tedge
# Cleanup file
sudo rm -f /run/lock/tedge/example.lock
echo "tedge is creating a lock"
sudo -u tedge sh -c "echo 'tedge' > /run/lock/tedge/example.lock" || echo "TEST FAILED"
echo "root is overwriting the lock"
sudo sh -c "echo 'pwned' > /run/lock/tedge/example.lock" || echo "TEST FAILED"
}
test_case_7() {
echo
echo "--------------------------------------------------------"
echo "Test case 7: stick flag ON, chmod 666 - non root user"
echo "--------------------------------------------------------"
cleanup
echo Should succeed
sudo chmod o+t /run/lock
sudo -u tedge timeout 3 tedge-agent
sudo chmod 666 /run/lock/tedge-agent.lock
sudo -u pi timeout 3 tedge-agent
}
test_case_8() {
echo
echo "--------------------------------------------------------"
echo "Test case 8: stick flag ON - non root user"
echo "--------------------------------------------------------"
cleanup
echo Should fail
sudo chmod o+t /run/lock
sudo -u tedge timeout 3 tedge-agent
sudo -u pi timeout 3 tedge-agent
}
# ---------
# main
# ---------
test_case_1
test_case_2
test_case_3
test_case_4
test_case_5
test_case_6
test_case_7
test_case_8
@didier-wenzek
Copy link

The line to be changed is https://gist.github.com/reubenmiller/94f8abac5457f2b0bd88c7b5bf4bef91#file-lock-testcases-sh-L98

test_case_6() {
    echo
    echo "--------------------------------------------------------"
    echo "Test case 6: Use subfolder with sticky flag OFF"
    echo "--------------------------------------------------------"
    # cleanup
    sudo rm -rf /run/lock/tedge

    sudo chmod o+t /run/lock
    

    echo "Create subfolder with chmod 777 and without sticky"
    sudo mkdir -p /run/lock/tedge
    sudo chmod 777 /run/lock/tedge

    # Technically this is not needed, but used to ensure that the
    # subfolder definitely does not
    sudo chmod o-t /run/lock/tedge

    # Cleanup file 
    sudo rm -f /run/lock/tedge/example.lock
    
    echo "tedge is creating a lock"
    sudo -u tedge sh -c "echo 'tedge' > /run/lock/tedge/example.lock" || echo "TEST FAILED"

    echo "root is overwriting the lock"
    sudo sh -c "echo 'pwned' >> /run/lock/tedge/example.lock" || echo "TEST FAILED"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment