Submitted by 0xdeadbeef0x, also found by SmartSek, joestakey, and hansfriese
A reentrancy bug in LineOfCredit.sol allows the lender to steal other lenders tokens if they are lending the same tokens type (loss of funds).
The  reentrancy occurs in the _close(credit, id) function in LineOfCredit.sol. The credit[id] state variable is cleared only after sendings tokens to the lender.
https://github.com/debtdao/Line-of-Credit/blob/e8aa08b44f6132a5ed901f8daa231700c5afeb3a/contracts/modules/credit/LineOfCredit.sol#L483
Reentrancy is possible if the borrower is lending tokens that can change the control flow. Such tokens are based on ERC20 such as ERC777, ERC223 or other customized ERC20 tokens that alert the receiver of transactions.
Example of a real-world popular token that can change control flow is PNT (pNetwork).
As the protocol supports any token listed on the oracle, if the oracle currently supports (or will support in the future) a feed of the above tokens, the bug is exploitable.
If a reentrancy occurs in the _close(credit, id) function, the credit[id] state variable is cleared only after sendings tokens to the lender.
A lender can abuse this by reentrancy to close(id) and retrieve credit.deposit + credit.interestRepaid amount of credit.token. A lender can repeat these processes as long as LineOfCredit has funds available.
The POC will demonstrate the following flow:
Add the MockLender.sol to mock folder.
Add Token777.sol to mocks folder:
Add the following imports to LineOfCredit.t.sol:
Add the following test to LineOfCredit.t.sol:
To run the POC execute:
forge test -v
Expected output:
To get full trace execute:
forge test -vvvv
VS Code, Foundry.
Send tokens only at the end of _close(Credit memory credit, bytes32 id) or add a reentrancyGuard.
kibagateaux (Debt DAO) disputed and commented:
dmvt (judge) commented:
