US stocks close with gains on Middle East tensions, oil volatility