Yes and No... the rules (laws) don't prohibit this. You could basically place all 11 players on the goal line and it would go by the rules. Of course there are certain situations where you are forced to leave the box (e.g. a penalty against you), but you're not forced to play offensive.
However, the FIFA Code of Conduct says Play to Win (click):
Winning is the object of playing any game. Never set out to lose. If
you do not play to win, you are cheating your opponents, deceiving
those who are watching, and also fooling yourself. Never give up
against stronger opponents but never relent against weaker ones. It is
an insult to any opponent to play at less than full strength. Play to
win, until the final whistle.
This kinda forces you to not just clear the ball and stay behind. But there is no clear rule of how to do it, so it's a grey area and you can't really draw a line between acceptable and unacceptable. It really depends on the situation.
There have been games where teams just stood behind and cleared the ball, simply because they weren't able to withstand the pressure of the opponents. That's completely fine and acceptable. From my own experience, we've saved a draw by doing this when we were two men short after two red cards. There is no point in attacking at all costs and eventually losing the game.
Unacceptable is playing to lose on purpose or doing something as pointed out in this Q&A.