The muscles are called arrector pili and they pull the hair upright for both of those reasons and a few others.. let me know if you want to know more :)
Erector spinae
The erector spinae
The erector spinae is a group of muscles that run along the spine and help maintain posture by supporting the spine and keeping the body upright. These muscles also play a role in bending and rotating the spine.
The erector pilaris muscles.
erector spinae group
Rectus Abdominis
arrector pilli
Erector Spinae
erector spinae group
Erector Spinae (Lower back)
arrector pilli